home *** CD-ROM | disk | FTP | other *** search
/ Enter 2001 April / EnterCD4.iso / Update / SQL Server SP3 / sql70sp3i.exe / INSTALL / replcom.sql < prev    next >
Encoding:
Text File  |  1999-11-08  |  329.5 KB  |  10,439 lines

  1.  
  2. /*
  3. ** replcom.sql            1997/02/12 22:03
  4. **
  5. **
  6. ** Copyright Microsoft, Inc. 1998-1999
  7. ** All Rights Reserved.
  8. ** Use, duplication, or disclosure by the United States Government
  9. ** is subject to restrictions as set forth in subdivision (c) (1) (ii)
  10. ** of the Rights in Technical Data and Computer Software clause
  11. ** at CFR 252.227-7013. Microsoft, Inc. One Microsoft Way, Redmond WA
  12. ** 98052.  SQL SERVER 7.0.
  13. */
  14.  
  15. dump tran master with no_log
  16. go
  17.  
  18. exec dbo.sp_configure 'update',1
  19. go
  20. reconfigure with override
  21. go
  22.  
  23. set ANSI_NULLS off
  24. go
  25.  
  26. use master
  27. go
  28.  
  29. exec dbo.sp_MS_upd_sysobj_category 1 --Capture time for use at the end
  30. go
  31.  
  32.  
  33. dump tran master with no_log
  34. GO
  35.  
  36. /* 
  37. ** Drop the stored procedures in this script using the old dropping SP 
  38. ** and then drop itself
  39. */
  40. if exists (select * from sysobjects
  41.     where type = 'P'
  42.             and name = 'sp_MSdrop_replcom')
  43. begin
  44.     exec dbo.sp_MSdrop_replcom
  45.     drop procedure sp_MSdrop_replcom
  46. end
  47.  
  48. dump tran msdb with no_log
  49. GO
  50.  
  51. /*
  52. ** Create stored procedures to drop the stored procedures
  53. ** created by this script
  54. */
  55.  
  56. raiserror('Creating procedure sp_MSdrop_replcom', 0,1)
  57. GO
  58.  
  59. create procedure sp_MSdrop_replcom
  60. as
  61.     if exists (select * from sysobjects 
  62.         where type = 'P'
  63.             and name = 'sp_MScreate_distributor_tables')
  64.         drop procedure sp_MScreate_distributor_tables
  65.  
  66.     if exists (select * from sysobjects
  67.         where type = 'P'
  68.                 and name = 'sp_MSIfExistsRemoteLogin')
  69.         drop procedure sp_MSIfExistsRemoteLogin
  70.  
  71.     if exists (select * from sysobjects
  72.         where type = 'P'
  73.                 and name = 'sp_helppublicationsync')
  74.         drop procedure sp_helppublicationsync
  75.  
  76.     if exists (select * from sysobjects
  77.         where type = 'P'
  78.                 and name = 'sp_replicationdboption')
  79.         drop procedure sp_replicationdboption
  80.  
  81.     if exists (select * from sysobjects
  82.             where type = 'P'
  83.                 and name = 'sp_addpublication_snapshot')
  84.         drop procedure sp_addpublication_snapshot
  85.  
  86.     if exists (select * from sysobjects
  87.             where type = 'P'
  88.                 and name = 'sp_MShelpobjectpublications')
  89.         drop procedure sp_MShelpobjectpublications
  90.  
  91.     if exists (select * from sysobjects
  92.             where type = 'P'
  93.                 and name = 'sp_addpublisher')
  94.         drop procedure sp_addpublisher
  95.  
  96.     if exists (select * from sysobjects
  97.             where type = 'P'
  98.                 and name = 'sp_addsubscriber')
  99.         drop procedure sp_addsubscriber
  100.  
  101.     if exists (select * from sysobjects
  102.             where type = 'P'
  103.                 and name = 'sp_addsubscriber_schedule')
  104.         drop procedure sp_addsubscriber_schedule
  105.  
  106.     if exists (select * from sysobjects
  107.             where type = 'P'
  108.                 and name = 'sp_changesubscriber_schedule')
  109.         drop procedure sp_changesubscriber_schedule
  110.  
  111.     if exists (select * from sysobjects
  112.             where type = 'P'
  113.                 and name = 'sp_changesubscriber')
  114.         drop procedure sp_changesubscriber
  115.  
  116.     IF EXISTS (SELECT * FROM sysobjects
  117.             WHERE type = 'P'
  118.                 AND name = 'sp_MScreate_dist_tables')
  119.         DROP PROCEDURE sp_MScreate_dist_tables
  120.  
  121.     if exists (select * from sysobjects
  122.             where type = 'P'
  123.                 and name = 'sp_distcounters')
  124.         drop procedure sp_distcounters
  125.  
  126.  
  127.     if exists (select * from sysobjects
  128.             where type = 'P'
  129.                 and name = 'sp_droppublisher')
  130.         drop procedure sp_droppublisher
  131.  
  132.     if exists (select * from sysobjects
  133.             where type = 'P'
  134.                 and name = 'sp_dropsubscriber')
  135.         drop procedure sp_dropsubscriber
  136.  
  137.     if exists (select * from sysobjects
  138.             where type = 'P'
  139.                 and name = 'sp_dsninfo')
  140.         drop procedure sp_dsninfo
  141.  
  142.     if exists (select * from sysobjects
  143.             where type = 'P'
  144.                 and name = 'sp_enumdsn')
  145.         drop procedure sp_enumdsn
  146.  
  147.     if exists (select * from sysobjects
  148.             where type = 'P'
  149.                 and name = 'sp_helpdistributor')
  150.         drop procedure sp_helpdistributor
  151.  
  152.     if exists (select * from sysobjects
  153.             where type = 'P'
  154.                 and name = 'sp_helppublicationsync')    /* Remove old version */
  155.         drop procedure sp_helppublicationsync
  156.  
  157.     if exists (select * from sysobjects
  158.             where type = 'P'
  159.                 and name = 'sp_helpreplicationdb')
  160.         drop procedure sp_helpreplicationdb
  161.  
  162.     if exists (select * from sysobjects
  163.             where type = 'P'
  164.                 and name = 'sp_helpsubscriberinfo')
  165.         drop procedure sp_helpsubscriberinfo
  166.  
  167.     if exists (select * from sysobjects
  168.             where type = 'P'
  169.                 and name = 'sp_publishdb')
  170.         drop procedure sp_publishdb
  171.  
  172.     if exists (select * from sysobjects
  173.             where type = 'P'
  174.                 and name = 'sp_replica')
  175.         drop procedure sp_replica
  176.  
  177.     if exists (select * from sysobjects
  178.             where type = 'P'
  179.                 and name = 'sp_adddistributiondb')
  180.         drop procedure sp_adddistributiondb
  181.  
  182.     if exists (select * from sysobjects
  183.             where type = 'P'
  184.                 and name = 'sp_MSexists_file')
  185.         drop procedure sp_MSexists_file
  186.  
  187.     if exists (select * from sysobjects
  188.             where type = 'P'
  189.                 and name = 'sp_changedistributiondb')
  190.         drop procedure sp_changedistributiondb
  191.  
  192.     if exists (select * from sysobjects
  193.             where type = 'P'
  194.                 and name = 'sp_helpdistributiondb')
  195.         drop procedure sp_helpdistributiondb
  196.  
  197.     if exists (select * from sysobjects
  198.             where type = 'P'
  199.                 and name = 'sp_dropdistributiondb')
  200.         drop procedure sp_dropdistributiondb
  201.  
  202.     if exists (select * from sysobjects
  203.             where type = 'P'
  204.                 and name = 'sp_adddistpublisher')
  205.         drop procedure sp_adddistpublisher
  206.  
  207.     if exists (select * from sysobjects
  208.             where type = 'P'
  209.                 and name = 'sp_changedistpublisher')
  210.         drop procedure sp_changedistpublisher
  211.  
  212.     if exists (select * from sysobjects
  213.             where type = 'P'
  214.                 and name = 'sp_helpdistpublisher')
  215.         drop procedure sp_helpdistpublisher
  216.  
  217.     if exists (select * from sysobjects
  218.             where type = 'P'
  219.                 and name = 'sp_dropdistpublisher')
  220.         drop procedure sp_dropdistpublisher
  221.  
  222.     if exists (select * from sysobjects
  223.             where type = 'P'
  224.                 and name = 'sp_MSadd_distributor_alerts_and_responses')
  225.         drop procedure  sp_MSadd_distributor_alerts_and_responses
  226.  
  227.     if exists (select * from sysobjects
  228.             where type = 'P'
  229.                 and name = 'sp_MSdrop_distributor_alerts_and_responses')
  230.         drop procedure  sp_MSdrop_distributor_alerts_and_responses
  231.  
  232.     if exists (select * from sysobjects
  233.             where type = 'P'
  234.                 and name = 'sp_adddistributor')
  235.         drop procedure  sp_adddistributor
  236.  
  237.     if exists (select * from sysobjects
  238.             where type = 'P'
  239.                 and name = 'sp_dropdistributor')
  240.         drop procedure sp_dropdistributor
  241.  
  242.     if exists (select * from sysobjects
  243.             where type = 'P'
  244.                 and name = 'sp_changedistributor_property')
  245.         drop procedure sp_changedistributor_property
  246.  
  247.     if exists (select * from sysobjects
  248.             where type = 'P'
  249.                 and name = 'sp_helpdistributor_properties')
  250.         drop procedure sp_helpdistributor_properties
  251.  
  252.     if exists (select * from sysobjects
  253.             where type = 'X'
  254.                 and name = 'sp_repldone')
  255.         exec dbo.sp_dropextendedproc 'sp_repldone'
  256.  
  257.     if exists (select * from sysobjects
  258.             where type = 'X'
  259.                 and name = 'sp_repltrans')
  260.         exec dbo.sp_dropextendedproc 'sp_repltrans'
  261.  
  262.     if exists (select * from sysobjects
  263.             where type = 'X'
  264.                 and name = 'sp_replcmds')
  265.         exec dbo.sp_dropextendedproc 'sp_replcmds'
  266.  
  267.     if exists (select * from sysobjects
  268.             where type = 'X'
  269.                 and name = 'sp_replcounters')
  270.         exec dbo.sp_dropextendedproc 'sp_replcounters'
  271.  
  272.     if exists (select * from sysobjects
  273.             where type = 'X'
  274.                 and name = 'sp_replflush')
  275.         exec dbo.sp_dropextendedproc 'sp_replflush'
  276.  
  277.     if exists (select * from sysobjects
  278.             where type = 'X'
  279.                  and name = 'sp_replpostcmd' )
  280.         exec dbo.sp_dropextendedproc 'sp_replpostcmd'
  281.  
  282.     if exists (select * from sysobjects
  283.             where type = 'X'
  284.                  and name = 'sp_replincrementlsn' )
  285.         exec dbo.sp_dropextendedproc 'sp_replincrementlsn'
  286.  
  287.     if exists (select * from sysobjects
  288.             where type = 'X'
  289.                  and name = 'sp_replupdateschema' )
  290.         exec dbo.sp_dropextendedproc 'sp_replupdateschema'
  291.  
  292.     if exists (select * from sysobjects
  293.             where type = 'X'
  294.                  and name = 'sp_replsetoriginator' )
  295.         exec dbo.sp_dropextendedproc 'sp_replsetoriginator'
  296.  
  297.     if exists (select * from sysobjects
  298.             where type = 'X'
  299.                 and name = 'xp_enumdsn')
  300.         exec dbo.sp_dropextendedproc 'xp_enumdsn'
  301.  
  302.     if exists (select * from sysobjects
  303.             where type = 'X'
  304.                 and name = 'xp_oledbinfo')
  305.         exec dbo.sp_dropextendedproc 'xp_oledbinfo'
  306.  
  307.     if exists (select * from sysobjects
  308.             where type = 'X'
  309.                 and name = 'xp_dsninfo')
  310.         exec dbo.sp_dropextendedproc 'xp_dsninfo'
  311.  
  312.     if exists (select * from sysobjects
  313.             where type = 'X'
  314.                 and name = 'xp_repl_encrypt')
  315.         exec dbo.sp_dropextendedproc 'xp_repl_encrypt'
  316.  
  317.     -- sp_helpsubscriber is removed permanently from the system.
  318.     if exists (select * from sysobjects
  319.         where type = 'P'
  320.                 and name = 'sp_helpsubscriber')
  321.         drop procedure sp_helpsubscriber
  322.  
  323.     -- sp_MSrepl_encrypt obsolete; use xp_repl_encrypt
  324.     if exists (select * from sysobjects
  325.         where type = 'P'
  326.                 and name = 'sp_MSrepl_encrypt')
  327.         drop procedure sp_MSrepl_encrypt
  328.  
  329.     if exists (select * from sysobjects 
  330.         where name = 'sp_add_agent_profile' 
  331.                 and type = 'P')
  332.         drop procedure sp_add_agent_profile
  333.  
  334.     if exists (select * from sysobjects 
  335.         where name = 'sp_help_agent_profile' 
  336.                 and type = 'P')
  337.         drop procedure sp_help_agent_profile
  338.  
  339.     if exists (select * from sysobjects 
  340.         where name = 'sp_help_agent_default' 
  341.             and type = 'P')
  342.         drop procedure sp_help_agent_default
  343.  
  344.     if exists (select * from sysobjects 
  345.         where name = 'sp_drop_agent_profile' 
  346.                 and type = 'P')
  347.         drop procedure sp_drop_agent_profile
  348.  
  349.     if exists (select name from sysobjects 
  350.         where name = 'sp_MSupdate_agenttype_default'
  351.                 and type = 'P')
  352.         drop procedure sp_MSupdate_agenttype_default
  353.  
  354.     if exists (select * from sysobjects 
  355.         where name = 'sp_MSvalidate_agent_parameter' 
  356.                 and type = 'P')
  357.         drop procedure sp_MSvalidate_agent_parameter
  358.  
  359.     if exists (select * from sysobjects 
  360.         where name = 'sp_add_agent_parameter' 
  361.                 and type = 'P')
  362.         drop procedure sp_add_agent_parameter
  363.  
  364.     if exists (select * from sysobjects 
  365.         where name = 'sp_generate_agent_parameter' 
  366.                 and type = 'P')
  367.         drop procedure sp_generate_agent_parameter
  368.  
  369.     if exists (select * from sysobjects 
  370.         where name = 'sp_change_agent_parameter' 
  371.                 and type = 'P')
  372.         drop procedure sp_change_agent_parameter
  373.  
  374.     if exists (select * from sysobjects 
  375.         where name = 'sp_change_agent_profile' 
  376.                 and type = 'P')
  377.         drop procedure sp_change_agent_profile
  378.  
  379.     if exists (select * from sysobjects 
  380.         where name = 'sp_help_agent_parameter' 
  381.                 and type = 'P')
  382.       drop procedure sp_help_agent_parameter
  383.  
  384.     if exists (select * from sysobjects 
  385.         where name = 'sp_drop_agent_parameter' 
  386.                 and type = 'P')
  387.       drop procedure sp_drop_agent_parameter
  388.  
  389.     if exists (select * from sysobjects 
  390.         where name = 'sp_MShelp_distdb' 
  391.                 and type = 'P')
  392.       drop procedure sp_MShelp_distdb
  393.  
  394.     if exists (select * from sysobjects 
  395.         where name = 'sp_MShelp_distdb' 
  396.                 and type = 'P')
  397.       drop procedure sp_MShelp_distdb
  398.  
  399.     if exists (select * from sysobjects 
  400.         where name = 'sp_MSenum_misc_agents'
  401.                 and type = 'P')
  402.       drop procedure sp_MSenum_misc_agents
  403.  
  404.     if exists (select * from sysobjects 
  405.         where name = 'sp_MSupdate_replication_status' 
  406.                 and type = 'P')
  407.       drop procedure sp_MSupdate_replication_status
  408.  
  409.     if exists (select * from sysobjects 
  410.         where name = 'sp_MSload_replication_status' 
  411.                 and type = 'P')
  412.       drop procedure sp_MSload_replication_status
  413.  
  414.     if exists (select * from sysobjects 
  415.         where name = 'sp_MScreate_replication_status_table' 
  416.                 and type = 'P')
  417.       drop procedure sp_MScreate_replication_status_table
  418.  
  419.     if exists (select * from sysobjects 
  420.         where name = 'sp_MShelp_replication_status' 
  421.                 and type = 'P')
  422.       drop procedure sp_MShelp_replication_status
  423.  
  424.     if exists (select * from sysobjects 
  425.         where name = 'sp_MSenum_replication_agents' 
  426.                 and type = 'P')
  427.       drop procedure sp_MSenum_replication_agents
  428.  
  429.     if exists (select * from sysobjects 
  430.         where name = 'sp_replication_agent_checkup' 
  431.                 and type = 'P')
  432.       drop procedure sp_replication_agent_checkup
  433.  
  434.     if exists (select * from sysobjects 
  435.         where name = 'sp_MSreplrole' 
  436.                 and type = 'P')
  437.       drop procedure sp_MSreplrole
  438.  
  439.     if exists (select * from sysobjects 
  440.         where name = 'sp_MScreate_replication_checkup_agent' 
  441.                 and type = 'P')
  442.       drop procedure sp_MScreate_replication_checkup_agent
  443.  
  444.     if exists (select * from sysobjects 
  445.         where name = 'sp_MSenum_replication_job' 
  446.                 and type = 'P')
  447.       drop procedure sp_MSenum_replication_job
  448.  
  449.     if exists (select * from sysobjects 
  450.         where name = 'sp_MSrepl_dbrole' 
  451.                 and type = 'P')
  452.       drop procedure sp_MSrepl_dbrole
  453.  
  454.     if exists (select * from sysobjects
  455.         where type = 'P'
  456.                 and name = 'sp_oledbinfo')
  457.         drop procedure sp_oledbinfo
  458.  
  459.     if exists (select * from sysobjects
  460.         where type = 'P'
  461.                 and name = 'sp_enumoledbdatasources')
  462.         drop procedure sp_enumoledbdatasources
  463.  
  464.     if exists (select * from sysobjects
  465.         where type = 'P'
  466.                 and name = 'sp_MSget_oledbinfo')
  467.         drop procedure sp_MSget_oledbinfo
  468.     
  469.     if exists (select * from sysobjects 
  470.         where name = 'sp_changedistributor_password' 
  471.                 and type = 'P')
  472.       drop procedure sp_changedistributor_password
  473.  
  474.     if exists (select * from sysobjects 
  475.         where name = 'sp_grant_publication_access' 
  476.                 and type = 'P')
  477.       drop procedure sp_grant_publication_access
  478.  
  479.     if exists (select * from sysobjects 
  480.         where name = 'sp_revoke_publication_access' 
  481.                 and type = 'P')
  482.       drop procedure sp_revoke_publication_access
  483.  
  484.     if exists (select * from sysobjects 
  485.         where name = 'sp_help_publication_access' 
  486.                 and type = 'P')
  487.       drop procedure sp_help_publication_access
  488.  
  489.     if exists (select * from sysobjects 
  490.         where name = 'sp_check_publication_access' 
  491.                 and type = 'P')
  492.       drop procedure sp_check_publication_access
  493.  
  494.     if exists (select * from sysobjects 
  495.         where name = 'sp_MSinit_replication_perfmon' 
  496.                 and type = 'P')
  497.       drop procedure sp_MSinit_replication_perfmon
  498.  
  499.     if exists (select * from sysobjects 
  500.         where name = 'sp_MSrepl_startup' 
  501.                 and type = 'P')
  502.       drop procedure sp_MSrepl_startup
  503.  
  504.     if exists (select * from sysobjects 
  505.         where name = 'sp_MSflush_access_cache' 
  506.                 and type = 'P')
  507.       drop procedure sp_MSflush_access_cache
  508.  
  509.     if exists (select * from sysobjects 
  510.         where name = 'sp_MSreinit_failed_subscriptions' 
  511.                 and type = 'P')
  512.       drop procedure sp_MSreinit_failed_subscriptions
  513.  
  514.     if exists (select * from sysobjects 
  515.         where name = 'sp_add_datatype_mapping' 
  516.                 and type = 'P')
  517.         drop procedure sp_add_datatype_mapping
  518.     
  519.     if exists (select * from sysobjects 
  520.         where name = 'sp_help_datatype_mapping' 
  521.                 and type = 'P')
  522.         drop procedure sp_help_datatype_mapping
  523.  
  524.     if exists (select * from sysobjects 
  525.         where name = 'sp_MSfix_6x_tasks' 
  526.                 and type = 'P')
  527.         drop procedure sp_MSfix_6x_tasks
  528. GO
  529.  
  530. /* 
  531. **  We must execute dbo.sp_MSdrop_replcom here since sp_MSdrop_replcom may not exist
  532. **  before this script is applied and it is possible
  533. **  that some replication sps are left. (We ignore error when dropping
  534. **  the replication so it is possible sp_MSdrop_replcom is dropped but
  535. **  some replication sps were left).
  536. */
  537. exec dbo.sp_MSdrop_replcom
  538. go
  539.  
  540. dump tran msdb with no_log
  541. GO
  542.  
  543. raiserror('Creating procedure sp_MScreate_distributor_tables', 0,1)
  544. GO
  545.  
  546. create procedure sp_MScreate_distributor_tables
  547. as
  548.     declare @profile_id     int
  549.     declare @retcode    int
  550.     declare @profile_name nvarchar(100)
  551.     declare @profile_desc nvarchar(100)
  552.  
  553.     /* Create MSpublishers table */
  554.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistpublishers'
  555.         and xtype = 'U')
  556.     begin
  557.         DROP TABLE msdb..MSdistpublishers
  558.         if @@error<> 0 goto FAILURE
  559.     end
  560.  
  561.     /* Create MSdistributiondbs table */
  562.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributiondbs'
  563.         and xtype = 'U')
  564.     begin
  565.         DROP TABLE msdb..MSdistributiondbs
  566.         if @@error<> 0 goto FAILURE
  567.     end
  568.  
  569.     /* create MSdistributor table */
  570.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributor'
  571.         and xtype = 'U')
  572.     begin
  573.         DROP TABLE msdb..MSdistributor
  574.         if @@error<> 0 goto FAILURE
  575.     end
  576.  
  577.     /* create sysreplicationalerts table */
  578.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'sysreplicationalerts'
  579.         and xtype = 'U')
  580.     begin
  581.         DROP TABLE msdb..sysreplicationalerts
  582.         if @@error<> 0 goto FAILURE
  583.     end
  584.     
  585.     /* create MSagent_profiles table */
  586.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_profiles'
  587.         and xtype = 'U')
  588.     begin
  589.         DROP TABLE msdb..MSagent_profiles
  590.         if @@error<> 0 goto FAILURE
  591.     end
  592.  
  593.     /* create MSagent_parameters table */
  594.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_parameters' 
  595.         and xtype = 'U')
  596.     begin
  597.         DROP TABLE msdb..MSagent_parameters
  598.         if @@error<> 0 goto FAILURE
  599.     end
  600.  
  601.     /* create MSdatatype_mappings table */
  602.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdatatype_mappings' 
  603.         and xtype = 'U')
  604.     begin
  605.         DROP TABLE msdb..MSdatatype_mappings
  606.         if @@error<> 0 goto FAILURE
  607.     end
  608.  
  609.     
  610.     CREATE TABLE msdb.dbo.MSdistpublishers
  611.     (
  612.         name                sysname     NOT NULL,
  613.         distribution_db     sysname     NOT NULL,
  614.         working_directory   nvarchar(255) NOT NULL,
  615.         security_mode       int         NOT NULL,
  616.         login               sysname     NOT NULL,
  617.         password            sysname     NULL,
  618.         active              bit         NOT NULL,
  619.         trusted             bit         NOT NULL,
  620.         thirdparty_flag     bit         NOT NULL
  621.     )
  622.     if @@error<> 0 goto FAILURE
  623.  
  624.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistpublishers ON msdb.dbo.MSdistpublishers(name)
  625.     if @@error<> 0 goto FAILURE
  626.  
  627.     CREATE TABLE msdb.dbo.MSdistributiondbs
  628.     (
  629.         name                sysname NOT NULL,
  630.         min_distretention   int     NOT NULL,
  631.         max_distretention   int     NOT NULL,
  632.         history_retention   int     NOT NULL
  633.     )
  634.     if @@error<> 0 goto FAILURE
  635.  
  636.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributiondbs ON msdb.dbo.MSdistributiondbs(name)
  637.     if @@error<> 0 goto FAILURE
  638.  
  639.     CREATE TABLE msdb.dbo.MSdistributor 
  640.     ( 
  641.         property        sysname     NOT NULL, 
  642.         value           nvarchar(3000)  NULL
  643.     )
  644.     if @@error<> 0 goto FAILURE
  645.  
  646.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributor ON msdb.dbo.MSdistributor(property)
  647.     if @@error<> 0 goto FAILURE
  648.  
  649.     CREATE TABLE msdb.dbo.sysreplicationalerts
  650.     (
  651.         alert_id            int identity(1,1) NOT NULL,
  652.         status              int NOT NULL,
  653.         agent_type          int NULL,
  654.         agent_id            int NULL,
  655.         error_id            int NULL,
  656.         alert_error_code    int NULL,
  657.         time                datetime NOT NULL,
  658.         publisher           sysname NULL,
  659.         publisher_db        sysname NULL,
  660.         publication         sysname NULL,
  661.         publication_type    int NULL,
  662.         subscriber          sysname NULL,
  663.         subscriber_db       sysname NULL,
  664.         article             sysname NULL,
  665.         destination_object  sysname NULL,
  666.         source_object       sysname NULL,
  667.         alert_error_text    ntext NULL
  668.     )
  669.     if @@error<> 0 goto FAILURE
  670.  
  671.     CREATE UNIQUE CLUSTERED INDEX ucsysreplicationalerts ON msdb.dbo.sysreplicationalerts(alert_id)
  672.     if @@error<> 0 goto FAILURE
  673.  
  674.     CREATE TABLE msdb.dbo.MSagent_profiles
  675.     (
  676.         profile_id    int           NOT NULL IDENTITY,
  677.         profile_name  sysname       NOT NULL,
  678.         agent_type          int             NOT NULL, -- 1-Snapshot, 2-Logreader,
  679.                                                       -- 3-Distribution, 4-Merge
  680.         type                int             NOT NULL, -- 0-System, 1-Custom
  681.         description         nvarchar(3000)  NULL,
  682.         def_profile   bit               NOT NULL
  683.     )
  684.     if @@error<> 0 goto FAILURE
  685.  
  686.  
  687.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_profiles ON msdb.dbo.MSagent_profiles
  688.         (profile_name, profile_id, agent_type)
  689.     if @@error<> 0 goto FAILURE
  690.  
  691.     CREATE TABLE msdb.dbo.MSagent_parameters
  692.     (
  693.         profile_id     int          NOT NULL,
  694.         parameter_name       sysname        NOT NULL,
  695.         value                nvarchar(255)  NOT NULL
  696.     )
  697.     if @@error<> 0 goto FAILURE
  698.  
  699.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_parameters ON msdb.dbo.MSagent_parameters
  700.         (parameter_name, profile_id)
  701.     if @@error<> 0 goto FAILURE
  702.  
  703.     /* 
  704.     ** Create default / non default profiles 
  705.     ** for all the agents 
  706.     */
  707.  
  708.     /* 
  709.     ** Snapshot agent 
  710.     */
  711.     set @profile_id = NULL
  712.     set @profile_name = formatmessage(20545) -- Default Snapshot Profile
  713.     set @profile_desc = NULL
  714.  
  715.     exec @retcode = dbo.sp_add_agent_profile
  716.             @profile_id = @profile_id OUT,
  717.             @profile_name = @profile_name,
  718.             @agent_type = 1,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  719.             @profile_type = 0,   -- 0-System, 1-Custom 
  720.             @description = @profile_desc,
  721.             @default = 1
  722.     if (@retcode = 1 or @@ERROR <> 0)
  723.         goto FAILURE
  724.  
  725.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  726.     if (@retcode = 1 or @@ERROR <> 0)
  727.         goto FAILURE
  728.  
  729.     /* 
  730.     ** Logreader agent 
  731.     */
  732.     set @profile_id = NULL
  733.     set @profile_name = formatmessage(20545) -- Default LogReader Profile
  734.     set @profile_desc = NULL
  735.  
  736.     exec @retcode = dbo.sp_add_agent_profile
  737.             @profile_id = @profile_id OUT,
  738.             @profile_name = @profile_name,
  739.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  740.             @profile_type = 0,   -- 0-System, 1-Custom 
  741.             @description = @profile_desc,
  742.             @default = 1
  743.     if (@retcode = 1 or @@ERROR <> 0)
  744.         goto FAILURE
  745.  
  746.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  747.     if (@retcode = 1 or @@ERROR <> 0)
  748.         goto FAILURE
  749.  
  750.     /* 
  751.     ** Logreader agent - Verbose History Profile
  752.     */
  753.     set @profile_id = NULL
  754.     set @profile_name = formatmessage(20546) -- LogReader Verbose History Profile
  755.     set @profile_desc = formatmessage(20547)
  756.  
  757.     exec @retcode = dbo.sp_add_agent_profile
  758.             @profile_id = @profile_id OUT,
  759.             @profile_name = @profile_name,
  760.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  761.             @profile_type = 0,   -- 0-System, 1-Custom 
  762.             @description = @profile_desc,
  763.             @default = 0
  764.     if (@retcode = 1 or @@ERROR <> 0)
  765.         goto FAILURE
  766.  
  767.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  768.     if (@retcode = 1 or @@ERROR <> 0)
  769.         goto FAILURE
  770.  
  771.     /* 
  772.     ** Distribution agent 
  773.     */
  774.     set @profile_id = NULL
  775.     set @profile_name = formatmessage(20545) -- Default Distribution Profile
  776.     set @profile_desc = NULL
  777.  
  778.     exec @retcode = dbo.sp_add_agent_profile
  779.             @profile_id = @profile_id OUT,
  780.             @profile_name = @profile_name,
  781.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  782.             @profile_type = 0,   -- 0-System, 1-Custom 
  783.             @description = @profile_desc,
  784.             @default = 1
  785.     if (@retcode = 1 or @@ERROR <> 0)
  786.         goto FAILURE
  787.  
  788.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  789.     if (@retcode = 1 or @@ERROR <> 0)
  790.         goto FAILURE
  791.  
  792.     /*
  793.     ** Distribution Agent Verbose History Profile
  794.     */
  795.     set @profile_id = NULL
  796.     set @profile_name = formatmessage(20546) -- Distribution Verbose History Profile
  797.     set @profile_desc = formatmessage(20547)
  798.  
  799.     exec @retcode = dbo.sp_add_agent_profile
  800.             @profile_id = @profile_id OUT,
  801.             @profile_name = @profile_name,
  802.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  803.             @profile_type = 0,   -- 0-System, 1-Custom 
  804.             @description = @profile_desc,
  805.             @default = 0
  806.     if (@retcode = 1 or @@ERROR <> 0)
  807.         goto FAILURE
  808.  
  809.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  810.     if (@retcode = 1 or @@ERROR <> 0)
  811.         goto FAILURE
  812.  
  813.     /* 
  814.     ** Merge agent : Default profile for well connected scenarios 
  815.     */
  816.     set @profile_id = NULL
  817.     set @profile_name = formatmessage(20545) -- Default Merge Profile
  818.     set @profile_desc = NULL
  819.  
  820.     exec @retcode = dbo.sp_add_agent_profile
  821.             @profile_id = @profile_id OUT,
  822.             @profile_name = @profile_name,
  823.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  824.             @profile_type = 0,   -- 0-System, 1-Custom 
  825.             @description = @profile_desc,
  826.             @default = 1
  827.  
  828.     if (@retcode = 1 or @@ERROR <> 0)
  829.         goto FAILURE
  830.  
  831.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  832.     if (@retcode = 1 or @@ERROR <> 0)
  833.         goto FAILURE
  834.  
  835.     /* 
  836.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  837.     */
  838.     set @profile_id = NULL
  839.     set @profile_name = formatmessage(20548) -- Non-Default Merge Profile
  840.     set @profile_desc = formatmessage(20549)
  841.  
  842.     exec @retcode = dbo.sp_add_agent_profile
  843.             @profile_id = @profile_id OUT,
  844.             @profile_name = @profile_name,
  845.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  846.             @profile_type = 0,   -- 0-System, 1-Custom 
  847.             @description = @profile_desc,
  848.             @default = 0
  849.  
  850.     if (@retcode = 1 or @@ERROR <> 0)
  851.         goto FAILURE
  852.  
  853.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  854.     if (@retcode = 1 or @@ERROR <> 0)
  855.         goto FAILURE
  856.  
  857.     /* 
  858.     ** Merge agent : Non default profile for verbose histroy
  859.     */
  860.     set @profile_id = NULL
  861.     set @profile_name = formatmessage(20546) -- Verbose Merge Profile
  862.     set @profile_desc = formatmessage(20547)
  863.  
  864.     exec @retcode = dbo.sp_add_agent_profile
  865.             @profile_id = @profile_id OUT,
  866.             @profile_name = @profile_name,
  867.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  868.             @profile_type = 0,   -- 0-System, 1-Custom 
  869.             @description = @profile_desc,
  870.             @default = 0
  871.  
  872.     if (@retcode = 1 or @@ERROR <> 0)
  873.         goto FAILURE
  874.  
  875.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  876.     if (@retcode = 1 or @@ERROR <> 0)
  877.         goto FAILURE
  878.  
  879.     /* 
  880.     ** Merge agent : Synchronization Manager Profile
  881.     */
  882.     set @profile_id = NULL
  883.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  884.     set @profile_desc = formatmessage(20551)
  885.  
  886.     exec @retcode = dbo.sp_add_agent_profile
  887.             @profile_id = @profile_id OUT,
  888.             @profile_name = @profile_name,
  889.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  890.             @profile_type = 0,   -- 0-System, 1-Custom 
  891.             @description = @profile_desc,
  892.             @default = 0
  893.  
  894.     if (@retcode = 1 or @@ERROR <> 0)
  895.         goto FAILURE
  896.  
  897.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  898.     if (@retcode = 1 or @@ERROR <> 0)
  899.         goto FAILURE
  900.  
  901.     /* 
  902.     ** Distribution agent : Synchronization Manager Profile
  903.     */
  904.     set @profile_id = NULL
  905.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  906.     set @profile_desc = formatmessage(20551)
  907.  
  908.     exec @retcode = dbo.sp_add_agent_profile
  909.             @profile_id = @profile_id OUT,
  910.             @profile_name = @profile_name,
  911.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  912.             @profile_type = 0,   -- 0-System, 1-Custom 
  913.             @description = @profile_desc,
  914.             @default = 0
  915.  
  916.     if (@retcode = 1 or @@ERROR <> 0)
  917.         goto FAILURE
  918.  
  919.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  920.     if (@retcode = 1 or @@ERROR <> 0)
  921.         goto FAILURE
  922.  
  923.  
  924.     create table msdb.dbo.MSdatatype_mappings 
  925.     (
  926.     dbms_name           sysname NOT NULL,
  927.     sql_type            sysname NOT NULL,
  928.     dest_type           sysname NOT NULL,
  929.     dest_prec           int     NOT NULL,
  930.     dest_create_params  int     NOT NULL,
  931.     dest_nullable       bit     NOT NULL
  932.     )
  933.  
  934.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'binary', 255, 4, 1
  935.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'varbinary', 255, 4, 1
  936.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'image', 1073741824, 0, 1
  937.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'image', 1073741824, 0, 1
  938.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'varchar', 255, 4, 1
  939.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'longtext', 1073741824, 0, 1
  940.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'nchar', 255, 4, 1
  941.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'longtext', 1073741824, 0, 1
  942.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'char', 255, 4, 1
  943.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'longtext', 1073741824, 0, 1
  944.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'nchar varying', 255, 4, 1
  945.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'longtext', 1073741824, 0, 1
  946.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'datetime' , 'datetime', 255, 0, 1
  947.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smalldatetime' , 'datetime', 255, 0, 1
  948.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'decimal' , 'decimal', 255, 3, 1
  949.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'numeric' , 'decimal', 255, 3, 1
  950.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'float' , 'float', 255, 0, 1
  951.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'real' , 'real', 255, 0, 1
  952.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'int' , 'int', 255, 0, 1
  953.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallint' , 'smallint', 255, 0, 1
  954.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'tinyint' , 'byte', 255, 0, 1
  955.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'money' , 'currency', 255, 0, 1
  956.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallmoney' , 'currency', 255, 0, 1
  957.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'bit' , 'bit', 255, 0, 1
  958.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'sysname' , 'nchar varying', 255, 4, 1
  959.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'timestamp' , 'binary', 255, 4, 1
  960.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'uniqueidentifier' , 'guid', 255, 0, 1
  961.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'text' , 'longtext', 1073741824, 0, 1
  962.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'ntext' , 'longtext', 1073741824, 0, 1
  963.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'image' , 'image', 1073741824, 0, 1
  964.  
  965.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'raw', 255, 4, 1
  966.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'raw', 255, 4, 1
  967.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'long raw', 2147483647, 0, 1
  968.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'long raw', 2147483647, 0, 1
  969.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'char', 255, 4, 1
  970.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'varchar2', 2000, 4, 1
  971.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'long', 2147483647, 0, 1
  972.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'char', 255, 4, 1
  973.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'varchar2', 2000, 4, 1
  974.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'long', 2147483647, 0, 1
  975.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'char', 255, 4, 1
  976.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'varchar2', 2000, 4, 1
  977.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'long', 2147483647, 0, 1
  978.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'char', 255, 4, 1
  979.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'varchar2', 2000, 4, 1
  980.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'long', 2147483647, 0, 1
  981.     exec dbo.sp_add_datatype_mapping 'Oracle', 'datetime' , 'char', 255, 4, 1
  982.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smalldatetime' , 'date', 255, 0, 1
  983.     exec dbo.sp_add_datatype_mapping 'Oracle', 'decimal' , 'number', 255, 3, 1
  984.     exec dbo.sp_add_datatype_mapping 'Oracle', 'numeric' , 'number', 255, 3, 1
  985.     exec dbo.sp_add_datatype_mapping 'Oracle', 'float' , 'float', 255, 0, 1
  986.     exec dbo.sp_add_datatype_mapping 'Oracle', 'real' , 'float', 255, 0, 1
  987.     exec dbo.sp_add_datatype_mapping 'Oracle', 'int' , 'number', 255, 3, 1
  988.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallint' , 'number', 255, 3, 1
  989.     exec dbo.sp_add_datatype_mapping 'Oracle', 'tinyint' , 'number', 255, 3, 1
  990.     exec dbo.sp_add_datatype_mapping 'Oracle', 'money' , 'number', 255, 3, 1
  991.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallmoney' , 'number', 255, 3, 1
  992.     exec dbo.sp_add_datatype_mapping 'Oracle', 'bit' , 'number', 255, 3, 1
  993.     exec dbo.sp_add_datatype_mapping 'Oracle', 'sysname' , 'char', 255, 4, 1
  994.     exec dbo.sp_add_datatype_mapping 'Oracle', 'timestamp' , 'raw', 255, 4, 1
  995.     exec dbo.sp_add_datatype_mapping 'Oracle', 'uniqueidentifier' , 'char', 255, 4, 1
  996.     exec dbo.sp_add_datatype_mapping 'Oracle', 'text' , 'long', 2147483647, 0, 1
  997.     exec dbo.sp_add_datatype_mapping 'Oracle', 'ntext' , 'long', 2147483647, 0, 1
  998.     exec dbo.sp_add_datatype_mapping 'Oracle', 'image' , 'long raw', 2147483647, 0, 1
  999.  
  1000.     return 0
  1001. FAILURE:
  1002.     return 1
  1003. go
  1004.  
  1005. dump tran master with no_log
  1006. GO
  1007.  
  1008. /*
  1009. ** Create replication stored procedures.
  1010. ** Part 2:  create all other stored procedures.
  1011. */
  1012.  
  1013. raiserror('Creating procedure sp_MSIfExistsRemoteLogin', 0,1)
  1014. GO
  1015.  
  1016. CREATE proc sp_MSIfExistsRemoteLogin
  1017.     @remotesrvname sysname,
  1018.     @locallgname   sysname,
  1019.     @remotelgname  sysname
  1020. AS
  1021. begin
  1022.  
  1023.  
  1024.     if (@locallgname IS NULL)
  1025.     begin
  1026.      IF EXISTS (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss
  1027.                  WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) AND 
  1028.                        srl.remoteserverid = ss.srvid AND 
  1029.                        (srl.remoteusername = @remotelgname OR 
  1030.                        (srl.remoteusername IS NULL AND srl.sid = 0x2)))
  1031.         return (1)
  1032.     else
  1033.         return (0)
  1034.     end
  1035.  
  1036.     if exists (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss 
  1037.                                 WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) AND 
  1038.                                       srl.remoteserverid = ss.srvid AND 
  1039.                                       srl.remoteusername = @remotelgname AND 
  1040.                                       srl.sid = suser_sid(@locallgname))
  1041.         return (1)
  1042.     else
  1043.         return (0)
  1044. end
  1045. go
  1046.  
  1047. raiserror('Creating procedure sp_helppublicationsync', 0,1)
  1048. GO
  1049.  
  1050. CREATE PROCEDURE sp_helppublicationsync (
  1051.         @publication sysname    /* The publication name */
  1052.         ) AS
  1053.     SET NOCOUNT ON
  1054.     RAISERROR (21023, 16, -1,'sp_helppublicationsync')
  1055.     RETURN(1)
  1056. GO
  1057.  
  1058. raiserror('Creating procedure sp_MSreplrole', 0,1)
  1059. GO
  1060. create procedure sp_MSreplrole
  1061. @name sysname,
  1062. @operation nvarchar(4)
  1063. as
  1064. declare @retcode int
  1065. -- Add/Drop when proper.
  1066. if @operation = 'add'
  1067. begin
  1068.     if user_id(@name) is null
  1069.     begin
  1070.         exec @retcode = dbo.sp_addrole @name
  1071.         IF @@ERROR <> 0 or @retcode <> 0
  1072.             RETURN (1)
  1073.     end
  1074. end
  1075. else if @operation = 'drop' 
  1076. begin
  1077.     if user_id(@name) is not null
  1078.     begin
  1079.         exec @retcode = dbo.sp_droprole @name
  1080.         IF @@ERROR <> 0 or @retcode <> 0
  1081.             RETURN (1)
  1082.     end
  1083. end
  1084. go
  1085.  
  1086.  
  1087. /* Create  sp_replicationdboption */
  1088. raiserror('Creating procedure sp_replicationdboption', 0,1)
  1089. GO
  1090.  
  1091. CREATE PROCEDURE sp_replicationdboption (
  1092.       @dbname    sysname,
  1093.       @optname   sysname,
  1094.       @value     sysname,
  1095.       @ignore_distributor bit = 0
  1096.     ) AS
  1097.  
  1098.     SET NOCOUNT ON
  1099.  
  1100.     /*
  1101.     ** Declarations.
  1102.     */
  1103.     
  1104.     declare @alert_name     sysname
  1105.     declare @alert_id       int
  1106.     declare @command        nvarchar(255)
  1107.     declare @description    nvarchar(500)
  1108.     declare @category_name  sysname
  1109.     declare @agentname      sysname
  1110.     DECLARE @retcode        int
  1111.     DECLARE @optbit         int
  1112.     DECLARE @optbit_value   int /* Desired value with the optbit mask */
  1113.     DECLARE @proc           nvarchar(255)
  1114.  
  1115.     /*
  1116.     ** Initialization
  1117.     */
  1118.  
  1119.     /*
  1120.     ** Parameter check
  1121.     ** @dbname
  1122.     */
  1123.     IF NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE
  1124.         name = @dbname)
  1125.     BEGIN
  1126.         RAISERROR(15010, 16, -1, @dbname)
  1127.         RETURN(1)
  1128.     END
  1129.  
  1130.     /*
  1131.     ** Parameter check
  1132.     ** @type
  1133.     */
  1134.     IF LOWER(@optname) NOT IN ('publish',
  1135.         'merge publish')
  1136.     BEGIN
  1137.         RAISERROR(14138,16,-1)
  1138.         RETURN(1)
  1139.     END
  1140.  
  1141.     /*
  1142.     ** Parameter check
  1143.     ** @value
  1144.     */
  1145.     IF LOWER(@value) NOT IN ('true','false')
  1146.     BEGIN
  1147.       RAISERROR(14137,16,-1)
  1148.       RETURN(1)
  1149.     END
  1150.  
  1151.     /*
  1152.     **  If we're in a transaction, disallow this since it might make recovery
  1153.     **  impossible.
  1154.     **
  1155.     */
  1156.     IF @@trancount > 0 
  1157.     BEGIN
  1158.         RAISERROR(15002,16,-1,'sp_replicationdboption')
  1159.         RETURN(1)
  1160.     END
  1161.     
  1162.     IF LOWER(@optname) = 'publish'
  1163.         BEGIN
  1164.             SELECT @optbit = 1
  1165.             SELECT @proc = QUOTENAME(@dbname) + '.dbo.sp_MSpublishdb'
  1166.         END
  1167.     ELSE IF LOWER(@optname) = 'merge publish'
  1168.         BEGIN
  1169.             SELECT @optbit = 4
  1170.             SELECT @proc = QUOTENAME(@dbname) + '.dbo.sp_MSmergepublishdb'
  1171.         END
  1172.  
  1173.     IF LOWER(@value) = 'true'
  1174.         SELECT @optbit_value = @optbit
  1175.     ELSE
  1176.         SELECT @optbit_value = 0
  1177.  
  1178.           
  1179.     /*
  1180.     ** Check if the option is set as required already
  1181.     */
  1182.     IF EXISTS (SELECT * FROM master..sysdatabases
  1183.         WHERE name = @dbname
  1184.         AND (category & @optbit) = @optbit_value)
  1185.     BEGIN
  1186.         if LOWER(@value) = 'true'
  1187.             RAISERROR (14035, 10, -1, @optname, @dbname)        
  1188.         else
  1189.             RAISERROR (14037, 10, -1, @optname, @dbname)
  1190.         RETURN (1)
  1191.     END
  1192.  
  1193.     /*
  1194.     ** Prepare the required option
  1195.     */
  1196.     EXEC @retcode = @proc @value = @value,
  1197.         @ignore_distributor = @ignore_distributor
  1198.     IF @@ERROR <> 0 or @retcode <> 0
  1199.     BEGIN
  1200.         GOTO UNDO
  1201.     END
  1202.  
  1203.  
  1204.     /*
  1205.     ** Preparation succeeded. 
  1206.     ** Toggle the category bit in master..sysdatabases
  1207.     */
  1208.     UPDATE master..sysdatabases SET category = category ^ @optbit
  1209.     WHERE name = @dbname
  1210.     IF @@ERROR <> 0 
  1211.     BEGIN
  1212.         GOTO UNDO
  1213.     END
  1214.  
  1215.     -- Get expired subscription cleanup agent name
  1216.     set @agentname = formatmessage(20569)  
  1217.  
  1218.     IF ((LOWER(@optname) = 'merge publish') or (LOWER(@optname) = 'publish')) and (LOWER(@value) = 'true')
  1219.     BEGIN
  1220.         IF NOT EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname and originating_server = '(local)')
  1221.             BEGIN
  1222.                 SELECT @command =  'EXEC dbo.sp_expired_subscription_cleanup'
  1223.             
  1224.                 set @description = formatmessage(20542)
  1225.     
  1226.                 select @category_name = name FROM msdb.dbo.syscategories where category_id = 17   
  1227.                 
  1228.                 EXECUTE @retcode = msdb.dbo.sp_MSadd_repl_job @agentname,
  1229.                 @subsystem = 'TSQL',
  1230.                 @server = @@SERVERNAME,
  1231.                 @databasename = @dbname,
  1232.                 @description = @description,
  1233.                 @freqtype = 4,        -- daily
  1234.                 @activestarttimeofday=010000,   -- from 01:00:00 am
  1235.                 @command = @command,
  1236.                 @enabled = 1,
  1237.                 @retryattempts = 0,
  1238.                 @loghistcompletionlevel = 0,
  1239.                 @category_name = @category_name
  1240.             
  1241.                 IF @@ERROR <> 0 or @retcode <> 0
  1242.                     BEGIN
  1243.                          return (1)
  1244.                     END
  1245.             END
  1246.  
  1247.         -- Expired subscription cleanup alert            
  1248.         select @category_name = name FROM msdb.dbo.syscategories where category_id = 20
  1249.         set @alert_name = formatmessage(20538)  
  1250.         set @alert_id = 14157 -- corresponding to formatmessage(20538)
  1251.         if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  1252.         begin
  1253.             exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14157
  1254.             if @@error <> 0 or @retcode <> 0
  1255.                 goto UNDO
  1256.         end
  1257.     END
  1258.  
  1259.     IF ((LOWER(@optname) = 'merge publish') or (LOWER(@optname) = 'publish')) and (LOWER(@value) = 'false')
  1260.     BEGIN
  1261.             IF (EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname and originating_server = '(local)')) 
  1262.                 and (NOT exists (select name from master..sysdatabases where category & 4 =4 ))
  1263.                 and (NOT exists (select name from master..sysdatabases where category & 1 =1))
  1264.         BEGIN
  1265.             EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @agentname
  1266.             IF @@ERROR <> 0 or @retcode <> 0
  1267.                 return (1)            
  1268.         END
  1269.         
  1270.         set @alert_id = 14157 -- cleanup  alert
  1271.         set @alert_name = formatmessage(20569)
  1272.         if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  1273.             and (NOT exists (select name from master..sysdatabases where category & 4 =4 ))
  1274.                 and (NOT exists (select name from master..sysdatabases where category & 1 =1))
  1275.         begin
  1276.             select @alert_name=name from msdb.dbo.sysalerts where message_id=@alert_id
  1277.             exec @retcode = msdb.dbo.sp_delete_alert @alert_name
  1278.             if @@error <> 0 or @retcode <> 0
  1279.                 return (1)            
  1280.         end
  1281.     END
  1282.     
  1283.     /*
  1284.     **  ??? 
  1285.     ** CHECKPOINT the database that was changed. Make the change
  1286.     ** effective immediatly
  1287.     */
  1288.     CHECKPOINT
  1289.     IF @@ERROR <> 0 
  1290.     BEGIN
  1291.         RETURN(1)
  1292.     END
  1293.  
  1294.     RETURN(0)
  1295.  
  1296. UNDO:
  1297.     -- Create system table is not allowed in a multi-statement transactions.
  1298.     -- Drop the tables here
  1299.     IF LOWER(@value) = 'true'
  1300.         EXEC dbo.sp_replicationdboption 
  1301.           @dbname    = @dbname,
  1302.           @optname   = @optname,
  1303.           @value     = 'false',
  1304.           @ignore_distributor = @ignore_distributor
  1305.  
  1306.     return(1)   
  1307. GO
  1308.  
  1309.  
  1310. dump tran master with no_log
  1311. GO
  1312.  
  1313. raiserror('Creating procedure sp_addpublication_snapshot', 0,1)
  1314. GO
  1315.  
  1316. CREATE PROCEDURE sp_addpublication_snapshot (
  1317.     @publication sysname,    
  1318.     @frequency_type  int = 4 ,              /* 4== Daily */
  1319.     @frequency_interval int = 1,            /* Every day */
  1320.     @frequency_subday int = 4,              /* Sub interval = Minute */
  1321.     @frequency_subday_interval int = 5,     /* Every five minutes */
  1322.     @frequency_relative_interval int = 1, 
  1323.     @frequency_recurrence_factor int = 0, 
  1324.     @active_start_date int = 0, 
  1325.     @active_end_date int = 99991231 , 
  1326.     @active_start_time_of_day int = 0, 
  1327.     @active_end_time_of_day int = 235959         
  1328. ) AS
  1329.  
  1330.     SET NOCOUNT ON
  1331.  
  1332.     /*
  1333.     ** Declarations.
  1334.     */
  1335.     DECLARE @retcode                int
  1336.     DECLARE @newid                  int
  1337.     DECLARE @mergepublish_bit       smallint
  1338.     DECLARE @transpublish_bit       int
  1339.     DECLARE @transpub_found         bit
  1340.     DECLARE @mergepub_found         bit
  1341.     DECLARE @newagentid             int
  1342.  
  1343.     /*
  1344.     ** Initializations
  1345.     */
  1346.     select @mergepublish_bit    = 4
  1347.     select @transpublish_bit    = 1
  1348.     select @transpub_found      = 0
  1349.     select @mergepub_found      = 0
  1350.     
  1351.     /*
  1352.     ** Parameter Check
  1353.     */
  1354.     exec @retcode = dbo.sp_MSreplcheck_name @publication
  1355.     if @@ERROR <> 0 or @retcode <> 0
  1356.         return(1)
  1357.  
  1358.     /*
  1359.     ** Security Check
  1360.     */
  1361.     exec @retcode = dbo.sp_MSreplcheck_publish
  1362.     if @@ERROR <> 0 or @retcode <> 0
  1363.         return(1)
  1364.  
  1365.     /*
  1366.     **  Check if the publication is valid.
  1367.     **  1. Check transaction-level publications
  1368.     **  2. Check merge publications
  1369.     */
  1370.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME()) <> 0
  1371.     begin
  1372.         EXEC @retcode = dbo.sp_MSaddpub_snapshot     @publication ,    
  1373.             @frequency_type,  
  1374.             @frequency_interval, 
  1375.             @frequency_subday, 
  1376.             @frequency_subday_interval,
  1377.             @frequency_relative_interval, 
  1378.             @frequency_recurrence_factor, 
  1379.             @active_start_date, 
  1380.             @active_end_date, 
  1381.             @active_start_time_of_day, 
  1382.             @active_end_time_of_day,         
  1383.             @newagentid OUTPUT
  1384.  
  1385.         IF @retcode <> 0 and @@ERROR <> 0 
  1386.             BEGIN
  1387.                 RETURN (1)
  1388.             END
  1389.  
  1390.         if @newagentid <> 0
  1391.             begin
  1392.                 select @transpub_found = 1
  1393.                 goto DONE
  1394.             end             
  1395.  
  1396.     end
  1397.     
  1398.     if (select category & @mergepublish_bit from master..sysdatabases where name = DB_NAME()) <> 0
  1399.     begin
  1400.         EXEC @retcode = dbo.sp_MSaddmergepub_snapshot     @publication ,    
  1401.             @frequency_type,  
  1402.             @frequency_interval, 
  1403.             @frequency_subday, 
  1404.             @frequency_subday_interval,
  1405.             @frequency_relative_interval, 
  1406.             @frequency_recurrence_factor, 
  1407.             @active_start_date, 
  1408.             @active_end_date, 
  1409.             @active_start_time_of_day, 
  1410.             @active_end_time_of_day,         
  1411.             @newagentid OUTPUT
  1412.  
  1413.         IF @retcode <> 0 and @@ERROR <> 0 
  1414.             BEGIN
  1415.                 RETURN (1)
  1416.             END
  1417.  
  1418.         if @newagentid <> 0
  1419.             begin
  1420.                 select @mergepub_found = 1
  1421.                 goto DONE
  1422.             end             
  1423.     end
  1424.  
  1425. DONE:
  1426.     if @transpub_found = 0 and @mergepub_found = 0
  1427.     begin
  1428.         RAISERROR (15001, 11, -1, @publication)
  1429.         RETURN (1)
  1430.     end
  1431.     return (0)        
  1432. GO
  1433.  
  1434. raiserror('Creating procedure sp_MShelpobjectpublications', 0,1)
  1435. GO
  1436.  
  1437. create procedure sp_MShelpobjectpublications (@object_name sysname)
  1438. AS
  1439.     /*
  1440.     ** Declarations.
  1441.     */
  1442.     DECLARE @retcode                int
  1443.     DECLARE @mergepublish_bit       smallint
  1444.     DECLARE @transpublish_bit       int
  1445.  
  1446.     declare @object_id int
  1447.  
  1448.     /*
  1449.     ** Initializations
  1450.     */
  1451.     select @mergepublish_bit    = 4
  1452.     select @transpublish_bit    = 1
  1453.     select @object_id           = OBJECT_ID(@object_name)
  1454.  
  1455.     create table #helpobjpubs (
  1456.         publication     sysname     NOT NULL, 
  1457.         reptype         int             NOT NULL, 
  1458.         article         sysname     NOT NULL, 
  1459.         article_type    int             NULL, 
  1460.         column_tracking int             NULL, 
  1461.         article_resolver nvarchar(255)  NULL)
  1462.     /*
  1463.     **  1. Return the transactional publications that the table is involved in
  1464.     */
  1465.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME()) <> 0
  1466.     begin
  1467.         if exists (select * from sysarticles a, 
  1468.             syspublications p where a.pubid = p.pubid and a.objid = @object_id)
  1469.             begin
  1470.                 insert into #helpobjpubs(publication, reptype, article, article_type) 
  1471.                     select p.name, 1, a.name, a.type  from sysarticles a, 
  1472.                         syspublications p where a.pubid = p.pubid and a.objid = @object_id
  1473.                 IF @@ERROR <> 0 
  1474.                     BEGIN
  1475.                         select @retcode = 1
  1476.                         goto DONE
  1477.                     END
  1478.             end
  1479.     end
  1480.     /*
  1481.     **  2. Return the merge publications that the table is involved in
  1482.     */
  1483.     if (select category & @mergepublish_bit from master..sysdatabases where name = DB_NAME()) <> 0
  1484.     begin
  1485.         if exists (select * from sysmergearticles a, 
  1486.             sysmergepublications p where a.pubid = p.pubid and a.objid = @object_id)
  1487.             begin
  1488.                 insert into #helpobjpubs (publication, reptype, article, article_type, column_tracking, article_resolver)
  1489.                     select p.name, 2, a.name, a.type, a.column_tracking, a.article_resolver 
  1490.                         from sysmergearticles a, sysmergepublications p 
  1491.                         where a.pubid = p.pubid and a.objid = @object_id
  1492.                 IF @@ERROR <> 0 
  1493.                     BEGIN
  1494.                         select @retcode = 1
  1495.                         goto DONE
  1496.                     END
  1497.             end
  1498.     end
  1499.     select @retcode = 0
  1500.  
  1501. DONE:
  1502.     select * from #helpobjpubs
  1503.     drop table #helpobjpubs
  1504.     return (@retcode)
  1505. go
  1506.  
  1507. raiserror('Creating procedure sp_helpreplicationdb', 0,1)
  1508. GO
  1509.  
  1510. CREATE PROCEDURE sp_helpreplicationdb
  1511.         @dbname sysname = '%', @type sysname = 'pub'
  1512.     AS
  1513.  
  1514.     SET NOCOUNT ON
  1515.  
  1516.     /*
  1517.     ** Declarations.
  1518.     */
  1519.  
  1520.     DECLARE @retcode int, @typebit int
  1521.  
  1522.     if (lower(@type) like 'pub%')
  1523.        select @typebit = 1
  1524.     else if (lower(@type) like 'sub%')
  1525.        select @typebit = 2
  1526.     else
  1527.     begin
  1528.        raiserror(14091,-1,-1)
  1529.        return 1
  1530.     end
  1531.  
  1532.     /*
  1533.     ** Parameter Check:  @dbname.
  1534.     ** Check to make sure that the database name conforms to the rules
  1535.     ** for identifiers.
  1536.     */
  1537.  
  1538.     IF @dbname <> '%'
  1539.        BEGIN
  1540.           EXECUTE @retcode = dbo.sp_validname @dbname
  1541.  
  1542.           IF @@ERROR <> 0 OR @retcode <> 0
  1543.           RETURN (1)
  1544.        END
  1545.  
  1546.     /*
  1547.     ** Show databases with this option enabled.
  1548.     */
  1549.  
  1550.     SELECT name
  1551.       FROM master..sysdatabases
  1552.      WHERE name LIKE @dbname
  1553.        AND (category & @typebit) <> 0
  1554. go
  1555.  
  1556.  
  1557. raiserror('Creating procedure sp_helpdistributor', 0,1)
  1558. go
  1559.  
  1560. CREATE PROCEDURE sp_helpdistributor (
  1561.     @distributor sysname  = '%' OUTPUT, /* The distribution server name */
  1562.     @distribdb   sysname  = '%' OUTPUT, /* The distribution database */
  1563.     @directory   nvarchar(255) = '%' OUTPUT, /* The working directory */
  1564.     @account     nvarchar(255) = '%' OUTPUT, /* The Windows NT user account */
  1565.     @min_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1566.     @max_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1567.     @history_retention   int  = -1 OUTPUT, /* The history retention period */
  1568.     @history_cleanupagent nvarchar(100) = '%' OUTPUT, /* The history cleanup agent */
  1569.     @distrib_cleanupagent nvarchar(100) = '%' OUTPUT, /* The distribution cleanup agent */
  1570.     @publisher sysname = NULL,  /* Name of publisher */
  1571.     @local nvarchar(5) = NULL,        /* Get local server values */
  1572.     @rpcsrvname sysname = '%' OUTPUT
  1573. ) AS
  1574.     SET NOCOUNT ON
  1575.  
  1576.     /*
  1577.     ** Declarations.
  1578.     */
  1579.     DECLARE @loc_distributor         sysname
  1580.     DECLARE @loc_distribdb             sysname
  1581.     DECLARE @loc_directory             nvarchar(255)
  1582.     DECLARE @loc_account             nvarchar(255)
  1583.     DECLARE @loc_mindistretention     int
  1584.     DECLARE @loc_maxdistretention     int
  1585.     DECLARE @loc_historyretention   int  
  1586.     DECLARE @loc_historycleanupagent nvarchar(100)
  1587.     DECLARE @loc_distribcleanupagent nvarchar(100)
  1588.     DECLARE @loc_security_mode  int  
  1589.     DECLARE @loc_login sysname
  1590.     DECLARE @loc_password sysname
  1591.     declare @loc_rpcsrvname sysname
  1592.     DECLARE @proc nvarchar(255)
  1593.     DECLARE @reg_key nvarchar (255)
  1594.     DECLARE @retcode int
  1595.     declare @rpcsrvlogin sysname
  1596.     declare @srvid smallint
  1597.     declare @dist_rpcname sysname
  1598.     declare @platform_nt binary
  1599.  
  1600.     select @platform_nt = 0x1
  1601.  
  1602.     IF @publisher IS NULL
  1603.     BEGIN
  1604.         /* 
  1605.         ** 6.x compatibility 
  1606.         ** If local is set, we know the call is from a publisher. 
  1607.         ** set it to be @@REMSERVER 
  1608.         ** Otherwise, set it to be local server name
  1609.         ** Note: @@REMSERVER is NULL for local sp calls 
  1610.         */
  1611.         IF LOWER(@local) = 'local' AND @@REMSERVER IS NOT NULL
  1612.             SELECT @publisher = @@REMSERVER
  1613.         ELSE
  1614.             SELECT  @publisher = @@SERVERNAME
  1615.     END
  1616.  
  1617.     /*
  1618.     ** Get the distribution server
  1619.     */
  1620.     SELECT @dist_rpcname = srvname, @loc_distributor = datasource, @srvid = srvid, @loc_rpcsrvname = srvname
  1621.         FROM master..sysservers
  1622.         WHERE srvstatus & 8 <> 0
  1623.     
  1624.     if @loc_distributor is null
  1625.         GOTO DONE
  1626.  
  1627.     select @rpcsrvlogin = name from master.dbo.sysxlogins where
  1628.         srvid = @srvid and sid is NULL
  1629.  
  1630.     /*
  1631.     ** If remote distribuiton, execute dbo.sp_helpdistributor on distribution
  1632.     ** server.
  1633.     */
  1634.     IF UPPER(@loc_distributor) <> UPPER(@@SERVERNAME)
  1635.     BEGIN
  1636.         SELECT @proc = @dist_rpcname + '.master.dbo.sp_helpdistributor'
  1637.         EXECUTE @retcode = @proc
  1638.             @loc_distributor OUTPUT,
  1639.             @loc_distribdb OUTPUT,
  1640.             @loc_directory OUTPUT,
  1641.             @loc_account OUTPUT,
  1642.             @loc_mindistretention OUTPUT,
  1643.             @loc_maxdistretention OUTPUT,
  1644.             @loc_historyretention OUTPUT,
  1645.             @loc_historycleanupagent OUTPUT,
  1646.             @loc_distribcleanupagent OUTPUT,
  1647.             @@SERVERNAME,
  1648.             @local = 'local'
  1649.         IF @retcode <> 0 or @@ERROR <> 0
  1650.            RETURN (1)
  1651.  
  1652.             GOTO DONE
  1653.     END
  1654.  
  1655.     SELECT  @loc_distribdb = distribution_db,
  1656.             @loc_directory = working_directory
  1657.         FROM msdb.dbo.MSdistpublishers WHERE UPPER(name) = UPPER(@publisher)
  1658.  
  1659.     IF @@ERROR <> 0
  1660.         RETURN 1 ;
  1661.  
  1662.     SELECT  @loc_mindistretention = min_distretention,      
  1663.             @loc_maxdistretention = max_distretention,
  1664.             @loc_historyretention = history_retention
  1665.         FROM msdb.dbo.MSdistributiondbs WHERE name = @loc_distribdb 
  1666.  
  1667.     /*
  1668.     ** Fetch the distribution account name.
  1669.     */
  1670.     IF ((@distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1671.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1672.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1673.     AND @distrib_cleanupagent = '%' ) 
  1674.     OR @account IS NULL) and ( platform() & @platform_nt = @platform_nt ) 
  1675.        BEGIN
  1676.         SELECT @proc = 'master..xp_regread'
  1677.         EXECUTE @retcode = @proc 'HKEY_LOCAL_MACHINE',
  1678.               'SYSTEM\CurrentControlSet\Services\SQLServerAgent',
  1679.               'ObjectName',
  1680.             @param = @loc_account OUTPUT
  1681.     
  1682.         IF @@ERROR <> 0 OR @retcode <> 0
  1683.             SELECT @loc_account = NULL
  1684.        END
  1685.  
  1686.     /*
  1687.     ** Fetch the history cleanup agentname.
  1688.     */
  1689.     IF @loc_distribdb IS NOT NULL
  1690.         SELECT @loc_historycleanupagent = formatmessage (20567, @loc_distribdb)
  1691.  
  1692.     /*
  1693.     ** Fetch the distribution cleanup agent name.
  1694.     */
  1695.     IF @loc_distribdb IS NOT NULL
  1696.         SELECT @loc_distribcleanupagent = formatmessage (20568, @loc_distribdb)
  1697.  
  1698.  
  1699. DONE:
  1700.     /*
  1701.     ** Return result set if no output parameters
  1702.     */
  1703.  
  1704.     IF @distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1705.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1706.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1707.     AND @distrib_cleanupagent = '%' AND @rpcsrvname = '%' 
  1708.     SELECT       'distributor'           = @loc_distributor,
  1709.                'distribution database' = @loc_distribdb,
  1710.                'directory'              = @loc_directory,
  1711.                'account'               = @loc_account,
  1712.                'min distrib retention' = @loc_mindistretention,
  1713.                'max distrib retention' = @loc_maxdistretention,
  1714.                'history retention'     = @loc_historyretention,
  1715.                'history cleanup agent'  = @loc_historycleanupagent,
  1716.                'distribution cleanup agent' = @loc_distribcleanupagent,
  1717.                'rpc server name' = @loc_rpcsrvname,
  1718.                'rpc login name' = @rpcsrvlogin
  1719.  
  1720.     /*
  1721.     ** Return output parameters if requested.
  1722.     */
  1723.  
  1724.     IF @distributor IS NULL
  1725.         SELECT @distributor = @loc_distributor
  1726.     IF @distribdb IS NULL
  1727.         SELECT @distribdb = @loc_distribdb
  1728.     IF @directory IS NULL
  1729.         SELECT @directory = @loc_directory
  1730.     IF @account IS NULL
  1731.         SELECT @account = @loc_account
  1732.     IF @min_distretention IS NULL
  1733.         SELECT @min_distretention = @loc_mindistretention
  1734.     IF @max_distretention IS NULL
  1735.         SELECT @max_distretention = @loc_maxdistretention
  1736.     IF @history_retention IS NULL
  1737.         SELECT @history_retention = @loc_historyretention
  1738.     IF @history_cleanupagent IS NULL
  1739.         SELECT @history_cleanupagent = @loc_historycleanupagent
  1740.     IF @distrib_cleanupagent IS NULL
  1741.         SELECT @distrib_cleanupagent = @loc_distribcleanupagent
  1742.    
  1743.     IF @rpcsrvname IS NULL
  1744.     begin
  1745.         -- Use local RPC if possible to avoid blocking problem.
  1746.         if is_srvrolemember('sysadmin') = 1 and UPPER(@loc_distributor) = UPPER(@@servername)
  1747.             select @rpcsrvname = @@servername
  1748.         else
  1749.             select @rpcsrvname = @loc_rpcsrvname
  1750.     end
  1751.   
  1752.     RETURN (0)
  1753. GO
  1754.  
  1755. dump tran master with no_log
  1756. go
  1757.  
  1758. raiserror('Creating procedure sp_enumdsn', 0,1)
  1759. GO
  1760.  
  1761. CREATE PROCEDURE sp_enumdsn
  1762.     AS
  1763.  
  1764.     SET NOCOUNT ON
  1765.  
  1766.     DECLARE @distributor sysname
  1767.     DECLARE @distproc nvarchar (255)
  1768.     DECLARE @retcode int
  1769.  
  1770.     DECLARE @dsotype_odbc int
  1771.     DECLARE @dsotype_oledb int
  1772.  
  1773.     select @dsotype_odbc = 1
  1774.     select @dsotype_oledb = 3
  1775.  
  1776.     /*
  1777.     ** Get distribution server information for remote RPC
  1778.     ** subscription calls.
  1779.     */
  1780.  
  1781.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  1782.     IF @@error <> 0 OR @retcode <> 0 or @distributor is null
  1783.         BEGIN
  1784.         RAISERROR (14071, 16, -1)
  1785.             RETURN (1)
  1786.     END
  1787.  
  1788.     create table #datasourcestemptable (DataSourceName sysname not null, Description varchar(255) null, DataSourceType int null, ProviderName varchar(255) null)
  1789.     
  1790.     /*
  1791.     ** Call xp_enumdsn
  1792.     */
  1793.     SELECT @distproc = RTRIM(@distributor) + '.master..xp_enumdsn'
  1794.     insert into #datasourcestemptable(DataSourceName, Description) EXEC @retcode = @distproc
  1795.     IF @@error <> 0
  1796.         BEGIN
  1797.         RAISERROR (14071, 16, -1)
  1798.         RETURN (1)
  1799.     END
  1800.  
  1801.     update #datasourcestemptable set DataSourceType = @dsotype_odbc where DataSourceType is null
  1802.     IF @@error <> 0 
  1803.     BEGIN
  1804.         RETURN (1)
  1805.     END
  1806.  
  1807.     /*
  1808.     ** Call sp_enumoledbdatasources
  1809.     */
  1810.     SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_enumoledbdatasources'
  1811.     insert into #datasourcestemptable(DataSourceName, Description, ProviderName) EXEC @retcode = @distproc
  1812.     IF @@error <> 0
  1813.         BEGIN
  1814.         RAISERROR (14071, 16, -1)
  1815.         RETURN (1)
  1816.     END
  1817.  
  1818.     update #datasourcestemptable set DataSourceType = @dsotype_oledb where DataSourceType is null
  1819.     IF @@error <> 0 
  1820.     BEGIN
  1821.         RETURN (1)
  1822.     END
  1823.     
  1824.     select 'Data Source Name' = DataSourceName, Description, 'Type' = DataSourceType, 'Provider Name' = ProviderName 
  1825.     from #datasourcestemptable
  1826.     order by 3, 1
  1827.  
  1828.     drop table #datasourcestemptable
  1829.  
  1830.     return (0)
  1831. go
  1832.  
  1833. raiserror('Creating procedure sp_enumoledbdatasources', 0,1)
  1834. GO
  1835.  
  1836. CREATE PROCEDURE sp_enumoledbdatasources
  1837.     AS
  1838.     set nocount on
  1839.  
  1840.     select srvname, srvproduct, providername from master..sysservers where 
  1841.     (srvstatus & 0x0080) <> 0
  1842.  
  1843.     return (0)
  1844. go
  1845.  
  1846. raiserror('Creating procedure sp_helpsubscriberinfo', 0,1)
  1847. GO
  1848.  
  1849. CREATE PROCEDURE sp_helpsubscriberinfo
  1850.     @subscriber sysname = '%'
  1851.     AS
  1852.  
  1853.     SET NOCOUNT ON
  1854.  
  1855.     DECLARE @distributor sysname
  1856.     DECLARE @distribdb sysname
  1857.     DECLARE @distproc nvarchar (255)
  1858.     DECLARE @retcode int
  1859.     DECLARE @subscriber_bit smallint
  1860.     DECLARE @show_password bit
  1861.  
  1862.     /*
  1863.     ** Security Check
  1864.     */
  1865.  
  1866.     /*
  1867.     ** Initializations.
  1868.     */
  1869.     SELECT @subscriber_bit = 4
  1870.  
  1871.  
  1872.     /*
  1873.     ** Check if subscriber is valid
  1874.     */
  1875.  
  1876.     IF @subscriber IS NULL
  1877.         BEGIN
  1878.             RAISERROR (14043, 16, -1, '@subscriber')
  1879.             RETURN (1)
  1880.         END
  1881.  
  1882.  
  1883.     IF @subscriber <> '%'
  1884.         BEGIN
  1885.  
  1886.             EXECUTE @retcode = dbo.sp_validname @subscriber
  1887.             IF @retcode <> 0
  1888.                 RETURN (1)
  1889.  
  1890.             IF NOT EXISTS (SELECT *
  1891.                         FROM master..sysservers
  1892.                         WHERE UPPER(srvname) = UPPER(@subscriber)
  1893.                         AND (srvstatus & @subscriber_bit) <> 0)
  1894.                 BEGIN
  1895.                     RAISERROR (14010, 16, -1)
  1896.                     RETURN (1)
  1897.                 END
  1898.         END
  1899.  
  1900.  
  1901.     /*
  1902.     ** Get distribution server information for remote RPC
  1903.     ** subscription calls.
  1904.     */
  1905.  
  1906.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  1907.                                        @distribdb   = @distribdb OUTPUT
  1908.  
  1909.     /* RAISEERROR is subscriber is not '%' */                                               
  1910.     IF @@error <> 0 OR @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  1911.         BEGIN
  1912.         IF @subscriber <> '%'
  1913.             BEGIN
  1914.                 RAISERROR (14071, 16, -1)
  1915.                 RETURN (1)
  1916.             END
  1917.         ELSE
  1918.             RETURN 0
  1919.         END
  1920.  
  1921.      create table #subscriber_info
  1922.      (
  1923.         publisher  sysname not null,
  1924.         subscriber  sysname not null,
  1925.         type tinyint not null,           /* 0: MS SQL Server 1: ODBC Data Source */
  1926.         login sysname NULL, 
  1927.         password sysname NULL,
  1928.         commit_batch_size int not null,  -- commit_batch_size, no longer supported
  1929.         status_batch_size int not null,  -- status_batch_size, no longer supported
  1930.         flush_frequency int not null,  -- flush_frequency, no longer supported
  1931.         frequency_type int not null,
  1932.         frequency_interval int not null,
  1933.         frequency_relative_interval int not null,
  1934.         frequency_recurrence_factor int not null,
  1935.         frequency_subday int not null,
  1936.         frequency_subday_interval int not null,
  1937.         active_start_time_of_day int not null,
  1938.         active_end_time_of_day int not null,
  1939.         active_start_date int not null,
  1940.         active_end_date int not null,
  1941.         retryattempt    int not null, -- retryattempt, no longer exist
  1942.         retrydelay  int not null,  -- retrydelay, no longer exist
  1943.         description nvarchar(255) NULL,
  1944.         security_mode int not null,
  1945.         frequency_type2 int not null,
  1946.         frequency_interval2 int not null,
  1947.         frequency_relative_interval2 int not null,
  1948.         frequency_recurrence_factor2 int not null,
  1949.         frequency_subday2 int not null,
  1950.         frequency_subday_interval2 int not null,
  1951.         active_start_time_of_day2 int not null,
  1952.         active_end_time_of_day2 int not null,
  1953.         active_start_date2 int not null,
  1954.         active_end_date2 int not null
  1955.     )
  1956.  
  1957.     IF @@error <> 0
  1958.         RETURN (1)
  1959.      
  1960.     /*
  1961.     ** Retrieve MSsubscriber_info
  1962.     */
  1963.     if is_srvrolemember('sysadmin') = 1
  1964.         select @show_password = 1
  1965.     else
  1966.         select @show_password = 0
  1967.  
  1968.     SELECT @distproc = RTRIM(@distributor) + '.' +
  1969.         RTRIM(@distribdb) + '.dbo.sp_MShelp_subscriber_info'
  1970.  
  1971.     insert into #subscriber_info EXEC @retcode = @distproc 
  1972.         @publisher = @@SERVERNAME, 
  1973.         @subscriber = @subscriber,
  1974.         @show_password = @show_password
  1975.  
  1976.     IF @@error <> 0
  1977.         BEGIN
  1978.         RAISERROR (14071, 16, -1)
  1979.         RETURN (1)
  1980.     END
  1981.  
  1982.     IF @retcode <> 0
  1983.         BEGIN
  1984.         RAISERROR (14085, 16, -1)
  1985.         RETURN (1)
  1986.      END
  1987.  
  1988.     -- Filter out subscribers that are not defined locally but left at the distributor
  1989.     -- This will happen if the publisher is cleaned up when the distributor is offline.
  1990.     select info.* from #subscriber_info info, master..sysservers servers where
  1991.         UPPER(info.subscriber) = UPPER(servers.srvname) and
  1992.         (servers.srvstatus & @subscriber_bit) <> 0
  1993.  
  1994. go
  1995.  
  1996. raiserror('Creating procedure sp_replica', 0,1)
  1997. GO
  1998.  
  1999. CREATE PROCEDURE sp_replica (
  2000.         @tabname nvarchar(92),     /* The table being replicated */
  2001.         @replicated nvarchar(5)    /* True or false */
  2002.         ) AS
  2003.  
  2004.     SET NOCOUNT ON
  2005.     RAISERROR (21023, 16, -1,'sp_replica')
  2006.     RETURN(1)
  2007. go
  2008.  
  2009.  
  2010. dump tran master with no_log
  2011. go
  2012.  
  2013. raiserror('Creating procedure sp_addpublisher', 0,1)
  2014. GO
  2015.  
  2016. CREATE PROCEDURE sp_addpublisher (
  2017.     @publisher sysname,      /* publisher server name */
  2018.     @type nvarchar (5) = NULL     /* NULL or 'dist' */
  2019.  ) AS
  2020.  
  2021.     SET NOCOUNT ON
  2022.     RAISERROR (21023, 16, -1,'sp_addpublisher')
  2023.     RETURN(1)
  2024. go
  2025.  
  2026. raiserror('Creating procedure sp_addsubscriber', 0,1)
  2027. GO
  2028.  
  2029. CREATE PROCEDURE sp_addsubscriber (
  2030.     @subscriber sysname,
  2031.     @type tinyint = 0,
  2032.     @login sysname = 'sa',
  2033.     @password sysname = NULL,
  2034.     @commit_batch_size int = 100,
  2035.     @status_batch_size int = 100,
  2036.     @flush_frequency int = 0,
  2037.     @frequency_type int = 64,
  2038.     @frequency_interval int = 1,
  2039.     @frequency_relative_interval int = 1,
  2040.     @frequency_recurrence_factor int = 0,
  2041.     @frequency_subday int = 4,
  2042.     @frequency_subday_interval int = 5,
  2043.     @active_start_time_of_day int = 0,
  2044.     @active_end_time_of_day int = 235959,
  2045.     @active_start_date int = 0,
  2046.     @active_end_date int = 99991231,
  2047.     @description nvarchar (255) = NULL,
  2048.     @security_mode int = 1,  /* backward compatible */ /* 0 standard; 1 integrated */
  2049.     @encrypted_password bit = 0
  2050.         ) AS
  2051.  
  2052.     DECLARE @distributor sysname
  2053.     DECLARE @distribdb sysname
  2054.     DECLARE @distproc nvarchar (255)
  2055.     DECLARE @retcode int
  2056.     DECLARE @dsn_subscriber tinyint
  2057.     DECLARE @jet_subscriber tinyint
  2058.     DECLARE @oledb_subscriber tinyint
  2059.     DECLARE @dist_rpcname sysname
  2060.     DECLARE @platform_nt binary
  2061.  
  2062.     -- Defined in sqlrepl.h
  2063.     select @dsn_subscriber = 1    /* Const: subscriber type 'dsn' */ 
  2064.     select @jet_subscriber = 2   
  2065.     select @oledb_subscriber = 3  
  2066.     
  2067.     select @platform_nt = 0x1
  2068.  
  2069.     /* 
  2070.     ** Check if replication components are installed on this server
  2071.     */
  2072.     exec @retcode = dbo.sp_MS_replication_installed
  2073.     if (@retcode <> 1)
  2074.     begin
  2075.         return (1)
  2076.     end
  2077.     
  2078.     /*
  2079.     ** Parameter Check:  @subscriber.
  2080.     ** Check to make sure that the subscriber doesn't already exist, and
  2081.     ** that the name is a valid non-null identifier.
  2082.     */
  2083.  
  2084.     IF @subscriber IS NULL
  2085.         BEGIN
  2086.             RAISERROR (14043, 16, -1, '@subscriber')
  2087.             RETURN (1)
  2088.         END
  2089.  
  2090.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2091.  
  2092.     IF @@ERROR <> 0 OR @retcode <> 0
  2093.     RETURN (1)
  2094.  
  2095.     if LOWER(@subscriber) = 'all'
  2096.         BEGIN
  2097.             RAISERROR (14032, 16, -1, '@subscriber')
  2098.             RETURN (1)
  2099.         END
  2100.  
  2101.     /* 
  2102.     ** Check for invalid security mode
  2103.     */
  2104.     IF @security_mode < 0 OR @security_mode > 1
  2105.         BEGIN
  2106.             RAISERROR(14109, 16, -1)
  2107.             RETURN (1)
  2108.         END
  2109.  
  2110.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2111.         BEGIN
  2112.             RAISERROR(21038, 16, -1)
  2113.             RETURN (1)
  2114.         END
  2115.  
  2116.     -- Clear the password if the security mode is 1
  2117.     if @security_mode = 1
  2118.         select @password = ''
  2119.  
  2120.     IF EXISTS (SELECT *
  2121.                  FROM master..sysservers
  2122.                 WHERE UPPER(srvname) = UPPER(@subscriber)
  2123.                   AND srvstatus & 4 <> 0)
  2124.  
  2125.         BEGIN
  2126.             RAISERROR (14040, 16, -1, @subscriber)
  2127.             RETURN (1)
  2128.         END
  2129.  
  2130.     IF @password = N''
  2131.         select @password = NULL
  2132.  
  2133.     
  2134.     /*
  2135.     **  If no MSsubscriber_info parameters skip RPC code.
  2136.     */
  2137.  
  2138.     IF @frequency_type = -1
  2139.         GOTO ADDSUB
  2140.  
  2141.     /*
  2142.     ** Get distribution server information for remote RPC
  2143.     ** subscription calls.
  2144.     */
  2145.  
  2146.  /*   BEGIN TRAN addsubscriber */
  2147.  
  2148.     EXEC @retcode = dbo.sp_helpdistributor  @distributor = @distributor OUTPUT,
  2149.                                         @rpcsrvname = @dist_rpcname OUTPUT,
  2150.                                         @distribdb   = @distribdb   OUTPUT
  2151.  
  2152.     IF @@error <> 0
  2153.         BEGIN
  2154.          RAISERROR (14071, 16, -1)
  2155.          goto undo
  2156.      END
  2157.  
  2158.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2159.         BEGIN
  2160.             RAISERROR (14071, 16, -1)
  2161.             goto undo
  2162.         END
  2163.  
  2164.  
  2165.     DECLARE @zeroint int
  2166.     SELECT @zeroint = 0
  2167.     /*
  2168.     ** Insert information into MSsubscriber_info
  2169.     */
  2170.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_info'
  2171.     EXEC @retcode = @distproc
  2172.          @@SERVERNAME,
  2173.          @subscriber,
  2174.      @type,
  2175.      @login,
  2176.      @password,
  2177.      @commit_batch_size,
  2178.      @status_batch_size,
  2179.      @flush_frequency,
  2180.      @frequency_type,
  2181.      @frequency_interval,
  2182.      @frequency_relative_interval,
  2183.      @frequency_recurrence_factor,
  2184.      @frequency_subday,
  2185.      @frequency_subday_interval,
  2186.      @active_start_time_of_day,
  2187.      @active_end_time_of_day,
  2188.      @active_start_date,
  2189.      @active_end_date,
  2190.      /* Work around of server RPC  named parameter problem */
  2191.      @retryattempts = @zeroint,
  2192.      @retrydelay  = @zeroint,
  2193.      @description = @description,
  2194.      @security_mode = @security_mode,
  2195.      @encrypted_password = @encrypted_password
  2196.  
  2197.     IF @@error <> 0 OR @retcode <> 0
  2198.         BEGIN
  2199.         RAISERROR (14042, 16, -1)
  2200.         goto undo
  2201.     END
  2202.  
  2203.     /*
  2204.     ** add schedule information into MSsubscriber_schedule for merge agent
  2205.     */
  2206.     -- NOTE: We may need better default schedule for merge agent
  2207.     
  2208.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2209.     EXEC @retcode = @distproc
  2210.          @@SERVERNAME,
  2211.          @subscriber,
  2212.          0, -- agent_type = 0 means distribution agent
  2213.          @frequency_type,
  2214.          @frequency_interval,
  2215.          @frequency_relative_interval,
  2216.          @frequency_recurrence_factor,
  2217.          @frequency_subday,
  2218.          @frequency_subday_interval,
  2219.          @active_start_time_of_day,
  2220.          @active_end_time_of_day,
  2221.          @active_start_date,
  2222.          @active_end_date
  2223.     
  2224.     IF @@error <> 0 OR @retcode <> 0
  2225.         BEGIN
  2226.             RAISERROR (14042, 16, -1)
  2227.             goto undo
  2228.         END
  2229.  
  2230.     EXEC @retcode = @distproc
  2231.          @@SERVERNAME,
  2232.          @subscriber,
  2233.          1,             --agent_type = 0 means merge agent
  2234.          4,             --frequency_type,
  2235.          1,             --frequency_interval,
  2236.          1,             --frequency_relative_interval,
  2237.          0,             --frequency_recurrence_factor,
  2238.          8,             --frequency_subday,
  2239.          1,             --frequency_subday_interval,
  2240.          0,             --active_start_time_of_day,
  2241.          235959,        --active_end_time_of_day,
  2242.          0,             --active_start_date,
  2243.          99991231       --active_end_date
  2244.     
  2245.     IF @@error <> 0 OR @retcode <> 0
  2246.         BEGIN
  2247.             RAISERROR (14042, 16, -1)
  2248.             goto undo
  2249.         END
  2250.  
  2251. /*    Commit Transaction addsubscriber */
  2252.  
  2253. ADDSUB:
  2254.  
  2255.     /*
  2256.     ** The server may already be listed in master..sysservers, but might
  2257.     ** not be marked as a subscriber yet.  If it's not in
  2258.     ** master..sysservers, let's add it first.
  2259.     */
  2260.  
  2261.     IF NOT EXISTS (SELECT *
  2262.                          FROM master..sysservers
  2263.                         WHERE LOWER(srvname) = LOWER(@subscriber))
  2264.     begin
  2265.  
  2266.         EXECUTE @retcode = dbo.sp_addserver @subscriber
  2267.  
  2268.             IF @@error <> 0 OR @retcode <> 0
  2269.                 BEGIN
  2270.                 RAISERROR (14042, 16, -1)
  2271.                     RETURN (1)
  2272.                 END
  2273.     end
  2274.     /*
  2275.     ** Set the server option to indicate this is a subscriber.
  2276.     */
  2277.  
  2278.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', true
  2279.  
  2280.     IF @@error <> 0 OR @retcode <> 0
  2281.         BEGIN
  2282.            RAISERROR (14042, 16, -1)
  2283.            RETURN (1)
  2284.         END
  2285.  
  2286.     /*
  2287.     ** Set the server option to indicate this is a DSN subscriber.
  2288.     */
  2289.     if @type = @dsn_subscriber OR @type = @jet_subscriber OR @type = @oledb_subscriber
  2290.        BEGIN
  2291.           update master..sysservers set srvproduct = 'MSREPL-NONSQL' where
  2292.             UPPER(srvname) = UPPER(@subscriber)
  2293.           IF @@error <> 0 
  2294.             return(1)
  2295.        END
  2296.     
  2297.         RETURN (0) 
  2298. undo:
  2299.  
  2300.     RETURN (1) 
  2301. GO
  2302.  
  2303. raiserror('Creating procedure sp_addsubscriber_schedule', 0,1)
  2304. GO
  2305.  
  2306. CREATE PROCEDURE sp_addsubscriber_schedule (
  2307.     @subscriber sysname,
  2308.     @agent_type smallint = 0,
  2309.     @frequency_type int = 64,
  2310.     @frequency_interval int = 1,
  2311.     @frequency_relative_interval int = 1,
  2312.     @frequency_recurrence_factor int = 0,
  2313.     @frequency_subday int = 4,
  2314.     @frequency_subday_interval int = 5,
  2315.     @active_start_time_of_day int = 0,
  2316.     @active_end_time_of_day int = 235959,
  2317.     @active_start_date int = 0,
  2318.     @active_end_date int = 99991231
  2319.         ) AS
  2320.  
  2321.     DECLARE @distributor sysname
  2322.     DECLARE @distribdb sysname
  2323.     DECLARE @distproc nvarchar (255)
  2324.     DECLARE @retcode int
  2325.     DECLARE @msg nvarchar(255)
  2326.     
  2327.     /*
  2328.     ** Parameter Check:  @subscriber.
  2329.     ** Check to make sure that the subscriber doesn't already exist, and
  2330.     ** that the name is a valid non-null identifier.
  2331.     */
  2332.  
  2333.     IF @subscriber IS NULL
  2334.         BEGIN
  2335.             RAISERROR (14043, 16, -1, '@subscriber')
  2336.             RETURN (1)
  2337.         END
  2338.  
  2339.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2340.  
  2341.     IF @@ERROR <> 0 OR @retcode <> 0
  2342.     RETURN (1)
  2343.  
  2344.     if LOWER(@subscriber) = 'all'
  2345.         BEGIN
  2346.             RAISERROR (14032, 16, -1, '@subscriber')
  2347.             RETURN (1)
  2348.         END
  2349.  
  2350.     IF NOT EXISTS (SELECT * FROM master..sysservers WHERE UPPER(srvname) = UPPER(@subscriber))
  2351.         BEGIN
  2352.             RAISERROR (14048, 16, -1, @subscriber)
  2353.             RETURN (1)
  2354.         END
  2355.     
  2356.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2357.                                        @distribdb   = @distribdb   OUTPUT
  2358.  
  2359.     IF @@error <> 0
  2360.         BEGIN
  2361.          RAISERROR (14071, 16, -1)
  2362.          RETURN (1)
  2363.      END
  2364.  
  2365.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2366.         BEGIN
  2367.             RAISERROR (14071, 16, -1)
  2368.             RETURN (1)
  2369.         END
  2370.     
  2371.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2372.     EXEC @retcode = @distproc
  2373.          @@SERVERNAME,
  2374.          @subscriber,
  2375.          @agent_type,
  2376.          @frequency_type,
  2377.          @frequency_interval,
  2378.          @frequency_relative_interval,
  2379.          @frequency_recurrence_factor,
  2380.          @frequency_subday,
  2381.          @frequency_subday_interval,
  2382.          @active_start_time_of_day,
  2383.          @active_end_time_of_day,
  2384.          @active_start_date,
  2385.          @active_end_date
  2386.     
  2387.     IF @@error <> 0 OR @retcode <> 0
  2388.         BEGIN
  2389.         RAISERROR (14042, 16, -1)
  2390.         RETURN (1)
  2391.     END
  2392.  
  2393.     RETURN (0)
  2394. GO
  2395.  
  2396. raiserror('Creating procedure sp_changesubscriber', 0,1)
  2397. GO
  2398.  
  2399. CREATE PROCEDURE sp_changesubscriber (
  2400.     @subscriber sysname,
  2401.     @type tinyint = NULL,
  2402.     @login sysname = NULL,
  2403.     @password sysname = '%',
  2404.     @commit_batch_size int = NULL,
  2405.     @status_batch_size int = NULL,
  2406.     @flush_frequency int = NULL,
  2407.     @frequency_type int = NULL,
  2408.     @frequency_interval int = NULL,
  2409.     @frequency_relative_interval int = NULL,
  2410.     @frequency_recurrence_factor int = NULL,
  2411.     @frequency_subday int = NULL,
  2412.     @frequency_subday_interval int = NULL,
  2413.     @active_start_time_of_day int = NULL,
  2414.     @active_end_time_of_day int = NULL,
  2415.     @active_start_date int = NULL,
  2416.     @active_end_date int = NULL,
  2417.     @description nvarchar (255) = NULL,
  2418.     @security_mode int = NULL /* 0 standard; 1 integrated */
  2419.  
  2420.         ) AS
  2421.  
  2422.     DECLARE @distributor sysname
  2423.     DECLARE @distribdb sysname
  2424.     DECLARE @distproc nvarchar (255)
  2425.     DECLARE @msg nvarchar(255)
  2426.     DECLARE @retcode int
  2427.     DECLARE @platform_nt binary
  2428.  
  2429.     select @platform_nt = 0x1
  2430.  
  2431.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2432.         BEGIN
  2433.             RAISERROR(21038, 16, -1)
  2434.             RETURN (1)
  2435.         END
  2436.  
  2437.     -- Clear the password if the security mode is 1
  2438.     if @security_mode = 1
  2439.         select @password = ''
  2440.  
  2441.     /*
  2442.     ** Check to make sure that the subscriber exists.
  2443.     */
  2444.     IF NOT EXISTS (SELECT *
  2445.                  FROM master..sysservers
  2446.                 WHERE UPPER(srvname) = UPPER(@subscriber))
  2447.         BEGIN
  2448.             RAISERROR(14048, 16, 1, @subscriber)
  2449.             RETURN (1)
  2450.         END
  2451.  
  2452.     /*
  2453.     ** Get distribution server information for remote RPC
  2454.     ** subscription calls.
  2455.     */
  2456.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2457.         @distribdb = @distribdb OUTPUT
  2458.     IF @@ERROR <> 0 OR @retcode <> 0
  2459.         BEGIN
  2460.             RAISERROR (14071, 16, -1)
  2461.             RETURN (1)
  2462.         END
  2463.  
  2464.     DECLARE @intnull int
  2465.     /*
  2466.     ** Update MSsubscriber_info
  2467.     */
  2468.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2469.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_info'
  2470.     EXEC @retcode = @distproc
  2471.         @@SERVERNAME,
  2472.         @subscriber,
  2473.     @type,
  2474.     @login,
  2475.     @password,
  2476.     @commit_batch_size,
  2477.     @status_batch_size,
  2478.     @flush_frequency,
  2479.     @frequency_type,
  2480.     @frequency_interval,
  2481.     @frequency_relative_interval,
  2482.     @frequency_recurrence_factor,
  2483.     @frequency_subday,
  2484.     @frequency_subday_interval,
  2485.     @active_start_time_of_day,
  2486.     @active_end_time_of_day,
  2487.     @active_start_date,
  2488.     @active_end_date,
  2489.     @retryattempts = @intnull,
  2490.     @retrydelay  = @intnull,
  2491.     @description = @description,
  2492.     @security_mode = @security_mode
  2493.     IF @@ERROR <> 0 OR @retcode <> 0
  2494.         BEGIN
  2495.             RAISERROR (14048, 16, -1, @subscriber)
  2496.             RETURN (1)
  2497.     END
  2498. go
  2499.  
  2500. raiserror('Creating procedure sp_changesubscriber_schedule', 0,1)
  2501. GO
  2502.  
  2503. CREATE PROCEDURE sp_changesubscriber_schedule (
  2504.     @subscriber sysname,
  2505.     @agent_type smallint,
  2506.     @frequency_type int = NULL,
  2507.     @frequency_interval int = NULL,
  2508.     @frequency_relative_interval int = NULL,
  2509.     @frequency_recurrence_factor int = NULL,
  2510.     @frequency_subday int = NULL,
  2511.     @frequency_subday_interval int = NULL,
  2512.     @active_start_time_of_day int = NULL,
  2513.     @active_end_time_of_day int = NULL,
  2514.     @active_start_date int = NULL,
  2515.     @active_end_date int = NULL
  2516.         ) AS
  2517.  
  2518.     DECLARE @distributor sysname
  2519.     DECLARE @distribdb sysname
  2520.     DECLARE @distproc nvarchar (255)
  2521.     DECLARE @msg nvarchar(255)
  2522.     DECLARE @retcode int
  2523.  
  2524.     /*
  2525.     ** Check to make sure that the subscriber DOES exist.
  2526.     */
  2527.     IF NOT EXISTS (SELECT *
  2528.                  FROM master..sysservers
  2529.                 WHERE UPPER(srvname) = UPPER(@subscriber))
  2530.         BEGIN
  2531.             RAISERROR (14048, 16, -1, @subscriber)
  2532.             RETURN (1)
  2533.         END
  2534.  
  2535.     /*
  2536.     ** Get distribution server information for remote RPC
  2537.     ** subscription calls.
  2538.     */
  2539.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2540.         @distribdb = @distribdb OUTPUT
  2541.     IF @@ERROR <> 0 OR @retcode <> 0
  2542.          BEGIN
  2543.             RAISERROR (14071, 16, -1)
  2544.              RETURN (1)
  2545.          END
  2546.  
  2547.     /*
  2548.     ** Update MSsubscriber_info
  2549.     */
  2550.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2551.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_schedule'
  2552.     EXEC @retcode = @distproc
  2553.         @@SERVERNAME,
  2554.         @subscriber,
  2555.         @agent_type,
  2556.         @frequency_type,
  2557.         @frequency_interval,
  2558.         @frequency_relative_interval,
  2559.         @frequency_recurrence_factor,
  2560.         @frequency_subday,
  2561.         @frequency_subday_interval,
  2562.         @active_start_time_of_day,
  2563.         @active_end_time_of_day,
  2564.         @active_start_date,
  2565.         @active_end_date
  2566.     
  2567.     IF @@ERROR <> 0 OR @retcode <> 0
  2568.         BEGIN
  2569.             RAISERROR (14048, 16, -1, @subscriber)
  2570.             RETURN (1)
  2571.         END
  2572.         
  2573.     RETURN (0)  
  2574. go 
  2575.     
  2576. raiserror('Creating procedure sp_distcounters', 0,1)
  2577. GO
  2578.  
  2579. CREATE PROCEDURE sp_distcounters
  2580.     AS
  2581.  
  2582.     SET NOCOUNT ON
  2583.  
  2584.     /*
  2585.     ** Declarations.
  2586.     */
  2587.     DECLARE @distributor sysname
  2588.     DECLARE @distribdb sysname
  2589.     DECLARE @distproc nvarchar (255)
  2590.     DECLARE @retcode int
  2591.  
  2592.     /*
  2593.     ** Get distribution server information for remote RPC
  2594.     ** subscription calls.  If no distribution information, assume
  2595.     ** replication is not being used.
  2596.     */
  2597.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2598.                                        @distribdb = @distribdb OUTPUT
  2599.     IF @@error <> 0 OR @retcode <> 0 OR @distributor IS NULL OR
  2600.        @distribdb IS NULL
  2601.     RETURN (1)
  2602.  
  2603.     /*
  2604.     ** Request counters from Distribution Server
  2605.     */
  2606.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) +
  2607.     '.dbo.sp_MSdistribution_counters'
  2608.     EXEC @retcode = @distproc @@SERVERNAME
  2609. go
  2610.  
  2611.  
  2612. raiserror('Creating procedure sp_droppublisher', 0,1)
  2613. GO
  2614.  
  2615. CREATE PROCEDURE sp_droppublisher (
  2616.     @publisher sysname,         /* publisher server name */
  2617.     @type nvarchar (5) = NULL      /* NULL or 'dist' */
  2618.         ) AS
  2619.  
  2620.     SET NOCOUNT ON
  2621.     RAISERROR (21023, 16, -1,'sp_droppublisher')
  2622.     RETURN(1)
  2623.  
  2624. go
  2625.  
  2626. raiserror('Creating procedure sp_dropsubscriber', 0,1)
  2627. GO
  2628.  
  2629. CREATE PROCEDURE sp_dropsubscriber (
  2630.     @subscriber sysname,        /* The name of the subscriber */
  2631.     @reserved nvarchar(50) = NULL,
  2632.     @ignore_distributor bit = 0
  2633.         ) AS
  2634.  
  2635.     SET NOCOUNT ON
  2636.  
  2637.     /*
  2638.     ** Declarations.
  2639.     */
  2640.  
  2641.     DECLARE @db_name sysname
  2642.     DECLARE @foundSubscription int
  2643.     DECLARE @command nvarchar(255)
  2644.     DECLARE @transpublishdb_bit int
  2645.     DECLARE @mergepublishdb_bit int
  2646.     DECLARE @distributor sysname
  2647.     DECLARE @distribdb sysname
  2648.     DECLARE @distproc nvarchar (255)
  2649.     DECLARE @retcode int
  2650.     DECLARE @type nvarchar(10)
  2651.  
  2652.     SELECT @transpublishdb_bit = 1
  2653.     SELECT @mergepublishdb_bit = 4
  2654.  
  2655.  
  2656.     /*
  2657.     ** Parameter Check:  @subscriber.
  2658.     ** Check to make sure that the subscriber exists.
  2659.     */
  2660.  
  2661.     IF @subscriber IS NULL
  2662.         BEGIN
  2663.             RAISERROR (14043, 16, -1, '@subscriber')
  2664.             RETURN (1)
  2665.         END
  2666.  
  2667.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2668.  
  2669.     IF @retcode <> 0
  2670.     RETURN (1)
  2671.  
  2672.     IF lower(@subscriber) <> 'all' and NOT EXISTS (SELECT *
  2673.                      FROM master..sysservers
  2674.                     WHERE UPPER(srvname) = UPPER(@subscriber)
  2675.                       AND srvstatus & 4 <> 0)
  2676.  
  2677.         BEGIN
  2678.             RAISERROR (14048, 16, -1, @subscriber)
  2679.             RETURN (1)
  2680.         END
  2681.  
  2682.     if lower(@subscriber) = 'all'
  2683.     begin
  2684.         DECLARE hCdropsubscriber_all CURSOR LOCAL FAST_FORWARD FOR
  2685.             SELECT srvname
  2686.                      FROM master..sysservers
  2687.                     WHERE srvstatus & 4 <> 0
  2688.         FOR READ ONLY
  2689.         OPEN hCdropsubscriber_all
  2690.         FETCH hCdropsubscriber_all INTO @subscriber
  2691.     
  2692.         WHILE (@@fetch_status <> -1)
  2693.         BEGIN
  2694.             EXECUTE @retcode  = dbo.sp_dropsubscriber
  2695.                 @subscriber = @subscriber,    
  2696.                 @ignore_distributor = @ignore_distributor,
  2697.                 @reserved = @reserved
  2698.             IF @@ERROR <> 0 OR @retcode <> 0
  2699.             BEGIN
  2700.                 CLOSE hCdropsubscriber_all
  2701.                 DEALLOCATE hCdropsubscriber_all
  2702.                 RETURN (1)
  2703.             END
  2704.             FETCH hCdropsubscriber_all INTO @subscriber
  2705.         end
  2706.         
  2707.         CLOSE hCdropsubscriber_all
  2708.         DEALLOCATE hCdropsubscriber_all
  2709.  
  2710.         return(0)
  2711.     end
  2712.  
  2713.  
  2714.     /*
  2715.     ** There should be no subscription by the subscriber
  2716.     ** Open a cursor the published databases.
  2717.     **
  2718.     */
  2719.     DECLARE hCdropsubscriber CURSOR LOCAL FAST_FORWARD FOR
  2720.             SELECT name, N'tran' FROM master..sysdatabases      
  2721.                 WHERE (category & @transpublishdb_bit) <> 0 
  2722.         UNION
  2723.             select name, N'merge' from master..sysdatabases      
  2724.                 WHERE (category & @mergepublishdb_bit) <> 0 
  2725.     FOR READ ONLY
  2726.  
  2727.     OPEN hCdropsubscriber
  2728.     FETCH hCdropsubscriber INTO @db_name, @type
  2729.     
  2730.     WHILE (@@fetch_status <> -1)
  2731.     BEGIN
  2732.         -- Bug 20323; used by UI
  2733.         IF LOWER(@reserved) = 'drop_subscriptions'
  2734.         BEGIN
  2735.             if @type = 'tran'
  2736.             begin
  2737.                 SELECT @command = @db_name + '.dbo.sp_dropsubscription'
  2738.                 EXECUTE @retcode  = @command 
  2739.                     @publication = 'all', 
  2740.                     @article = 'all', 
  2741.                     @subscriber = @subscriber,
  2742.                     @ignore_distributor = @ignore_distributor
  2743.             end
  2744.             if @type = 'merge'
  2745.             begin
  2746.                 SELECT @command = @db_name + '.dbo.sp_dropmergesubscription'
  2747.                 EXECUTE @retcode  = @command 
  2748.                     @publication = 'all', 
  2749.                     @subscriber = @subscriber,
  2750.                     @subscription_type = 'both',
  2751.                     @ignore_distributor = @ignore_distributor
  2752.             end
  2753.             IF @@ERROR <> 0 OR @retcode <> 0
  2754.             BEGIN
  2755.                 CLOSE hCdropsubscriber
  2756.                 DEALLOCATE hCdropsubscriber
  2757.                 RETURN (1)
  2758.             END
  2759.         END
  2760.         
  2761.  
  2762.         if @type = 'tran'
  2763.         begin
  2764.             SELECT @command = @db_name + '.dbo.sp_helpsubscription'
  2765.             EXECUTE @retcode  = @command @publication = '%', @article = '%', 
  2766.                 @subscriber = @subscriber,
  2767.                 @found = @foundSubscription  OUTPUT
  2768.  
  2769.             IF @@ERROR <> 0 OR @retcode <> 0
  2770.             BEGIN
  2771.                 CLOSE hCdropsubscriber
  2772.                 DEALLOCATE hCdropsubscriber
  2773.                 RETURN (1)
  2774.             END
  2775.         end
  2776.         else
  2777.         begin
  2778.             SELECT @command = @db_name + '.dbo.sp_helpmergesubscription'
  2779.             EXECUTE @retcode  = @command @publication = '%', 
  2780.                 @subscriber = @subscriber,
  2781.                 @subscription_type = 'both',
  2782.                 @found = @foundSubscription  OUTPUT
  2783.  
  2784.             IF @@ERROR <> 0 OR @retcode <> 0
  2785.             BEGIN
  2786.                 CLOSE hCdropsubscriber
  2787.                 DEALLOCATE hCdropsubscriber
  2788.                 RETURN (1)
  2789.             END
  2790.         end
  2791.  
  2792.  
  2793.         IF @foundSubscription <> 0
  2794.         BEGIN
  2795.             CLOSE hCdropsubscriber
  2796.             DEALLOCATE hCdropsubscriber
  2797.             RAISERROR ( 14144,  16, -1, @subscriber, @db_name)
  2798.             RETURN (1)
  2799.         END
  2800.  
  2801.         FETCH hCdropsubscriber INTO @db_name, @type
  2802.     END
  2803.     
  2804.     CLOSE hCdropsubscriber
  2805.     DEALLOCATE hCdropsubscriber
  2806.  
  2807.     /*
  2808.     ** Drop the subsubscriber_info in the distribution database
  2809.     */
  2810.     /*
  2811.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  2812.     */
  2813.     if @ignore_distributor = 0
  2814.     begin
  2815.  
  2816.  
  2817.         /*
  2818.         ** Get distribution server information for remote RPC
  2819.         ** agent verification.
  2820.         */
  2821.         EXEC @retcode = dbo.sp_helpdistributor 
  2822.             @rpcsrvname = @distributor OUTPUT,
  2823.             @distribdb = @distribdb OUTPUT
  2824.         IF @@error <> 0 OR @retcode <> 0
  2825.             BEGIN
  2826.             RAISERROR (14071, 16, -1)
  2827.                RETURN (1)
  2828.         END
  2829.         
  2830.         /*
  2831.         ** Insert information into MSsubscriber_info in the distribution db
  2832.         */
  2833.         if @distribdb is not null
  2834.         begin
  2835.             SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSdrop_subscriber_info'
  2836.             EXEC @retcode = @distproc
  2837.                  @publisher = @@SERVERNAME,
  2838.                  @subscriber = @subscriber
  2839.  
  2840.             IF @@error <> 0 OR @retcode <> 0
  2841.                 BEGIN
  2842.                 RAISERROR (14042, 16, -1)
  2843.                 RETURN (1)
  2844.             END
  2845.         end
  2846.     end
  2847.  
  2848.     /*
  2849.     ** Turn off the subscriber server option.
  2850.     */
  2851.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', false
  2852.     IF @@ERROR <> 0 OR @retcode <> 0
  2853.     BEGIN
  2854.        RAISERROR (14047, 16, -1, @subscriber)
  2855.        RETURN (1)
  2856.     END
  2857.  
  2858.     RAISERROR (14062, 10, -1)
  2859. go
  2860.  
  2861. raiserror('Creating procedure sp_dsninfo', 0,1)
  2862. GO
  2863.  
  2864. CREATE PROCEDURE sp_dsninfo
  2865. --  xp_dsninfo does not support unicode
  2866.     @dsn varchar(128),
  2867.     @infotype varchar(128) = NULL,
  2868.     @login varchar(128) = NULL,
  2869.     @password varchar(128) = NULL,
  2870.     @dso_type int = 1  /* 1 is ODBC, 3 OLEDB. */
  2871.     AS
  2872.  
  2873.     SET NOCOUNT ON
  2874.  
  2875.     DECLARE @distributor sysname
  2876.     DECLARE @distproc nvarchar (255)
  2877.     DECLARE @retcode int
  2878.     DECLARE @dsotype_odbc int
  2879.     DECLARE @dsotype_oledb int
  2880.  
  2881.     select @dsotype_odbc = 1
  2882.     select @dsotype_oledb = 3
  2883.  
  2884.     /*
  2885.     ** Get distribution server information for remote RPC
  2886.     ** subscription calls.
  2887.     */
  2888.  
  2889.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  2890.     IF @@error <> 0 OR @retcode <> 0
  2891.         BEGIN
  2892.         RAISERROR (14071, 16, -1)
  2893.             RETURN (1)
  2894.     END
  2895.  
  2896.     if (@dso_type = @dsotype_odbc)
  2897.     begin
  2898.         /*
  2899.         ** Call xp_dsninfo
  2900.         */
  2901.         SELECT @distproc = RTRIM(@distributor) + '.master..xp_dsninfo'
  2902.         EXEC @retcode = @distproc @dsn, @infotype, @login, @password
  2903.         IF @@error <> 0
  2904.             BEGIN
  2905.             RAISERROR (14071, 16, -1)
  2906.             RETURN (1)
  2907.         END
  2908.     end
  2909.     else if (@dso_type = @dsotype_oledb)
  2910.     begin
  2911.         /*
  2912.         ** Call sp_oledbinfo
  2913.         */
  2914.         EXEC @retcode = master.dbo.sp_oledbinfo @dsn, @infotype, @login, @password
  2915.         IF @@error <> 0
  2916.             BEGIN
  2917.             RAISERROR (14071, 16, -1)
  2918.             RETURN (1)
  2919.         END
  2920.     end
  2921.  
  2922. go
  2923.  
  2924. dump tran master with no_log
  2925. go
  2926.  
  2927. raiserror('Creating procedure sp_publishdb', 0,1)
  2928. GO
  2929.  
  2930. /* For backward compatible */
  2931. CREATE PROCEDURE sp_publishdb @dbname sysname,@value nvarchar (5)
  2932.     AS
  2933.  
  2934.     DECLARE @retcode int
  2935.  
  2936.     EXECUTE @retcode = dbo.sp_replicationdboption @dbname, 'publish', @value
  2937.  
  2938.     IF @@ERROR <> 0 or @retcode <> 0
  2939.     BEGIN
  2940.        RETURN (1)
  2941.     END
  2942.  
  2943.     RETURN(0)
  2944. go
  2945.  
  2946.  
  2947. raiserror('Creating procedure sp_MScreate_dist_tables', 0,1)
  2948. GO
  2949.  
  2950. CREATE PROCEDURE sp_MScreate_dist_tables
  2951. AS
  2952.  
  2953.    /*
  2954.    ** Important:  
  2955.    ** We use varbinary(16) for xact_id and xact_seqno, we don't want ending nulls
  2956.    ** to be truncated by the server
  2957.    **
  2958.    ** Also, in MSrepl_commands, we don't want ending space to be truncated.  
  2959.    */
  2960.    SET ANSI_PADDING ON
  2961.  
  2962.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_version' and type = 'U')
  2963.    BEGIN
  2964.       /****************************************************************************/
  2965.       raiserror('Creating table MSrepl_version', 0,1)
  2966.       /****************************************************************************/
  2967.       CREATE TABLE dbo.MSrepl_version
  2968.       (
  2969.       major_version int NOT NULL,
  2970.       minor_version int NOT NULL,
  2971.       revision int NOT NULL,
  2972.       db_existed bit NULL
  2973.       )
  2974.     
  2975.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_version'
  2976.       
  2977.       raiserror('Creating clustered index ucMSrepl_version', 0,1)    
  2978.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_version ON dbo.MSrepl_version
  2979.             (major_version, minor_version, revision)
  2980.  
  2981.       INSERT INTO MSrepl_version VALUES (7,0,0,0)
  2982.    END
  2983.  
  2984.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublisher_databases' and type = 'U')
  2985.    BEGIN
  2986.       /****************************************************************************/
  2987.       raiserror('Creating table MSpublisher_databases', 0,1)
  2988.       /****************************************************************************/
  2989.       CREATE TABLE dbo.MSpublisher_databases(
  2990.         publisher_id smallint NOT NULL,
  2991.         publisher_db sysname NULL,
  2992.         id int identity NOT NULL,                   
  2993.     )
  2994.       EXEC dbo.sp_MS_marksystemobject 'MSpublisher_databases'
  2995.  
  2996.       raiserror('Creating clustered index ucMSpublisher_databases', 0,1)
  2997.       CREATE UNIQUE CLUSTERED INDEX ucMSpublisher_databases ON dbo.MSpublisher_databases
  2998.      (publisher_id, publisher_db, id)
  2999.    END
  3000.  
  3001.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublications' and type = 'U')
  3002.    BEGIN
  3003.       /****************************************************************************/
  3004.       raiserror('Creating table MSpublications', 0,1)
  3005.       /****************************************************************************/
  3006.       CREATE TABLE dbo.MSpublications (
  3007.         publisher_id smallint NOT NULL,
  3008.         publisher_db sysname NULL,
  3009.         publication sysname NOT NULL,
  3010.         publication_id int identity NOT NULL,   -- This id IS NOT the same as the SQL Server publisher's
  3011.         publication_type int NOT NULL,          -- 0 = Snapshot 1 = Transactional
  3012.         thirdparty_flag bit NOT NULL,           -- 0 = SQL Server 1 = Third Party
  3013.         independent_agent bit NOT NULL,
  3014.         immediate_sync bit NOT NULL,
  3015.         allow_push bit NOT NULL,
  3016.         allow_pull bit NOT NULL,
  3017.         allow_anonymous bit NOT NULL,
  3018.         description nvarchar(255) NULL,
  3019.         vendor_name nvarchar(100) NULL,
  3020.         retention    int NULL
  3021.     )
  3022.       EXEC dbo.sp_MS_marksystemobject 'MSpublications'
  3023.  
  3024.      -- publication_id needs to be the first columns in the index. It
  3025.      -- is used in sp_MSmaximum_cleanup_seqno.
  3026.       raiserror('Creating clustered index ucMSpublications', 0,1)
  3027.       CREATE UNIQUE CLUSTERED INDEX ucMSpublications ON dbo.MSpublications
  3028.      (publication_id,  publication, publisher_db, publisher_id)
  3029.  
  3030.    END
  3031.  
  3032.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSarticles' and type = 'U')
  3033.    BEGIN
  3034.       /****************************************************************************/
  3035.       raiserror('Creating table MSarticles', 0,1)
  3036.       /****************************************************************************/
  3037.     CREATE TABLE dbo.MSarticles (
  3038.         publisher_id smallint NOT NULL,
  3039.         publisher_db sysname NULL,
  3040.         publication_id int NOT NULL,
  3041.         article sysname NOT NULL,
  3042.         article_id int NOT NULL,                            -- This id is the same as a SQL Server Publisher's
  3043.         destination_object sysname NULL,
  3044.         source_owner    sysname NULL,
  3045.         source_object sysname NULL,
  3046.         description nvarchar(255) NULL  
  3047.     )   
  3048.  
  3049.       EXEC dbo.sp_MS_marksystemobject 'MSarticles'
  3050.  
  3051.       raiserror('Creating clustered index ucMSarticles', 0,1)
  3052.       CREATE UNIQUE CLUSTERED INDEX ucMSarticles ON dbo.MSarticles
  3053.      (publisher_db, publisher_id, article_id, article, publication_id)
  3054.    END
  3055.  
  3056.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriptions' and type = 'U')
  3057.    BEGIN
  3058.       /****************************************************************************/
  3059.       raiserror('Creating table MSsubscriptions', 0,1)
  3060.       /****************************************************************************/
  3061.        CREATE TABLE dbo.MSsubscriptions
  3062.       (
  3063.         publisher_database_id int NOT NULL,         -- Used to reference MSrepl_transactions and MSrepl_commands
  3064.         publisher_id smallint NOT NULL,
  3065.         publisher_db sysname NOT NULL,
  3066.         publication_id int NOT NULL,
  3067.         article_id int NOT NULL,
  3068.         subscriber_id smallint NOT NULL,
  3069.         subscriber_db sysname NOT NULL,
  3070.         subscription_type int NOT NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3071.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3072.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3073.         subscription_seqno varbinary(16) NOT NULL,  -- publisher's database sequence number 
  3074.         snapshot_seqno_flag bit NOT NULL,               -- 1 if subscription_seqno is the snapshot seqno
  3075.         independent_agent bit NOT NULL,             -- Value carried over from MSpublications
  3076.         subscription_time datetime NOT NULL, 
  3077.         loopback_detection bit NOT NULL,  
  3078.         agent_id int NOT NULL,
  3079.         update_mode tinyint NOT NULL,
  3080.         publisher_seqno varbinary(16) NOT NULL
  3081.       )
  3082.  
  3083.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriptions'
  3084.  
  3085.       raiserror('Creating clustered index ucMSsubscirptions', 0,1)
  3086.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriptions ON dbo.MSsubscriptions
  3087.       (agent_id, article_id)
  3088.  
  3089.       -- Index used by sp_MSdelete_publisherdb_trans
  3090.       raiserror('Creating index iMSsubscriptions', 0,1)
  3091.       CREATE INDEX iMSsubscriptions ON dbo.MSsubscriptions
  3092.       (publisher_database_id, article_id, subscriber_id, subscriber_db, publication_id, publisher_db, publisher_id)
  3093.    END
  3094.    
  3095.    -- For beta 3 upgrade, we need to create new index here.
  3096.    if not exists (select * from sysindexes where name = 'iMSsubscriptions2')
  3097.    begin
  3098.       -- Index used by sp_MSdelete_publisherdb_trans
  3099.       raiserror('Creating index iMSsubscriptions2', 0,1)
  3100.       CREATE INDEX iMSsubscriptions2 ON dbo.MSsubscriptions
  3101.       (publisher_database_id, subscription_seqno)
  3102.    end
  3103.  
  3104.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_subscriptions' and type = 'U')
  3105.    BEGIN
  3106.       /****************************************************************************/
  3107.       raiserror('Creating table MSmerge_subscriptions', 0,1)
  3108.       /****************************************************************************/
  3109.        CREATE TABLE dbo.MSmerge_subscriptions
  3110.       (
  3111.         publisher_id smallint NOT NULL,
  3112.         publisher_db sysname NULL,
  3113.         publication_id int NOT NULL,
  3114.         subscriber_id smallint NOT NULL,
  3115.         subscriber_db sysname NULL,
  3116.         subscription_type int NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3117.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3118.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3119.         subscription_time datetime NOT NULL
  3120.       )
  3121.  
  3122.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_subscriptions'
  3123.  
  3124.       raiserror('Creating clustered index ucMSmerge_subscriptions', 0,1)
  3125.       CREATE UNIQUE CLUSTERED INDEX ucMSmerge_subscriptions ON dbo.MSmerge_subscriptions
  3126.      (publisher_id, publisher_db, publication_id, subscriber_id, subscriber_db)
  3127.    END
  3128.  
  3129.  
  3130.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_transactions' and type = 'U')
  3131.    BEGIN
  3132.       /****************************************************************************/
  3133.       raiserror('Creating table MSrepl_transactions', 0,1)
  3134.       /****************************************************************************/
  3135.       CREATE TABLE dbo.MSrepl_transactions
  3136.       (
  3137.       publisher_database_id int NOT NULL,
  3138.       xact_id varbinary(16) NULL,
  3139.       xact_seqno varbinary (16 )  NOT NULL,
  3140.       entry_time datetime  NOT NULL
  3141.       )
  3142.  
  3143.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_transactions'
  3144.  
  3145.       raiserror('Creating clustered index usMSrepl_transactions', 0,1)
  3146.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_transactions ON dbo.MSrepl_transactions
  3147.          (publisher_database_id, xact_seqno)
  3148.    END
  3149.  
  3150.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_commands' and type = 'U')
  3151.    BEGIN
  3152.  
  3153.       /****************************************************************************/
  3154.       raiserror('Creating table MSrepl_commands', 0,1)
  3155.       /****************************************************************************/
  3156.  
  3157.  
  3158.       CREATE TABLE dbo.MSrepl_commands (
  3159.       publisher_database_id int not null,
  3160.       xact_seqno varbinary(16) not null,
  3161.       type int not null, 
  3162.       article_id int not null,
  3163.       originator_id int not null,
  3164.       command_id int not null,
  3165.       partial_command bit not null,
  3166.       command varbinary(1024) NULL
  3167.       )
  3168.  
  3169.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_commands'
  3170.  
  3171.       raiserror('Creating clusterd index ucMSrepl_commands', 0,1)
  3172.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_commands ON dbo.MSrepl_commands
  3173.          (publisher_database_id, xact_seqno, command_id)
  3174.    END
  3175.  
  3176.  
  3177.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_originators' and type = 'U')
  3178.    BEGIN
  3179.       /****************************************************************************/
  3180.       raiserror('Creating table MSrepl_orginators', 0,1)
  3181.       /****************************************************************************/
  3182.       CREATE TABLE dbo.MSrepl_originators
  3183.       (
  3184.       id int identity not null,
  3185.       publisher_database_id int not null,
  3186.       srvname   sysname not null,
  3187.       dbname    sysname not null
  3188.       )
  3189.  
  3190.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_originators'
  3191.  
  3192.       raiserror('Creating clustered index usMSrepl_originators', 0,1)
  3193.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_originators ON dbo.MSrepl_originators
  3194.          (publisher_database_id, srvname, dbname)
  3195.    END
  3196.  
  3197.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_info' and type = 'U')
  3198.    BEGIN
  3199.       /****************************************************************************/
  3200.       raiserror('Creating table MSsubscriber_info', 0,1)
  3201.       /****************************************************************************/
  3202.       CREATE TABLE dbo.MSsubscriber_info
  3203.       (
  3204.       publisher  sysname NOT NULL,
  3205.       subscriber  sysname NOT NULL,
  3206.       type tinyint NOT NULL,           /* 0: MS SQL Server 1: ODBC Data Source */
  3207.       login sysname NULL,
  3208.       password sysname NULL,
  3209.       description nvarchar(255) NULL,
  3210.       security_mode int NOT NULL
  3211.       )
  3212.  
  3213.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_info'
  3214.  
  3215.       raiserror('Creating clustered index ucMSsubscriber_info', 0,1)
  3216.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_info ON dbo.MSsubscriber_info
  3217.      (publisher, subscriber)
  3218.    END
  3219.    ELSE
  3220.    BEGIN
  3221.       IF NOT EXISTS (select * from syscolumns
  3222.         where name = 'description'
  3223.         and id=object_id('MSsubscriber_info'))
  3224.       BEGIN
  3225.          
  3226.          ALTER TABLE MSsubscriber_info ADD description nvarchar (255) NULL
  3227.          UPDATE MSsubscriber_info SET description = 'SQL Server 6.0'
  3228.       END
  3229.    END
  3230.  
  3231.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_schedule' and type = 'U')
  3232.    BEGIN
  3233.          raiserror('Creating table MSsubscriber_schedule', 0,1)
  3234.          
  3235.         CREATE TABLE dbo.MSsubscriber_schedule
  3236.         (
  3237.             publisher  sysname NOT NULL,
  3238.             subscriber  sysname NOT NULL,
  3239.             agent_type      smallint NOT NULL,   -- 0 for distribution agent, 1 for merge agent
  3240.             frequency_type int NOT NULL,
  3241.             frequency_interval int NOT NULL,
  3242.             frequency_relative_interval int NOT NULL,
  3243.             frequency_recurrence_factor int NOT NULL,
  3244.             frequency_subday int NOT NULL,
  3245.             frequency_subday_interval int NOT NULL,
  3246.             active_start_time_of_day int NOT NULL,
  3247.             active_end_time_of_day int NOT NULL,
  3248.             active_start_date int NOT NULL,
  3249.             active_end_date int NOT NULL
  3250.         )
  3251.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_schedule'
  3252.  
  3253.         CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_schedule ON dbo.MSsubscriber_schedule
  3254.      (publisher, subscriber, agent_type)
  3255.      
  3256.     END
  3257.  
  3258.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_history' and type = 'U')
  3259.    BEGIN
  3260.         /****************************************************************************/
  3261.          raiserror('Creating table MSsnapshot_history', 0,1)
  3262.         /****************************************************************************/
  3263.         CREATE TABLE dbo.MSsnapshot_history 
  3264.         (
  3265.         agent_id int NOT NULL,
  3266.         runstatus int NOT NULL,
  3267.         start_time datetime NOT NULL,        
  3268.         time datetime NOT NULL,    
  3269.         duration int NOT NULL,            
  3270.         comments nvarchar(255) NOT NULL,
  3271.  
  3272.         -- Session summary statistics
  3273.         delivered_transactions int NOT NULL,
  3274.         delivered_commands int NOT NULL,
  3275.         delivery_rate float NOT NULL,
  3276.  
  3277.         error_id int NOT NULL,
  3278.         timestamp NOT NULL
  3279.     )
  3280.  
  3281.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_history'
  3282.  
  3283.      raiserror('Creating clustered index ucMSsnapshot_history', 0,1)
  3284.     CREATE UNIQUE CLUSTERED INDEX ucMSsnapshot_history ON dbo.MSsnapshot_history
  3285.         (agent_id, timestamp, start_time, time)
  3286.  
  3287.     if not exists (select * from sysindexes where name = 'nc1MSsnapshot_history')
  3288.     begin
  3289.          raiserror('Creating clustered index nc1MSsnapshot_history', 0,1)
  3290.         create nonclustered index nc1MSsnapshot_history on MSsnapshot_history(agent_id, start_time) 
  3291.     end    
  3292.  
  3293.    END
  3294.        
  3295.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_history' and type = 'U')
  3296.    BEGIN
  3297.         /****************************************************************************/
  3298.         raiserror('Creating table MSlogreader_history', 0,1)
  3299.         /****************************************************************************/    
  3300.         CREATE TABLE dbo.MSlogreader_history
  3301.         (
  3302.         agent_id int NOT NULL,
  3303.         runstatus int NOT NULL,
  3304.         start_time datetime NOT NULL,
  3305.         time datetime NOT NULL,
  3306.         duration int NOT NULL,
  3307.         comments nvarchar(255) NOT NULL,
  3308.         xact_seqno varbinary(16) NULL,
  3309.  
  3310.         -- Session summary statistics
  3311.         delivery_time int NOT NULL,
  3312.         delivered_transactions int NOT NULL,
  3313.         delivered_commands int NOT NULL,
  3314.         average_commands int NOT NULL,
  3315.         delivery_rate float NOT NULL,
  3316.         delivery_latency int NOT NULL,
  3317.  
  3318.         error_id int NOT NULL,
  3319.         timestamp NOT NULL
  3320.         )
  3321.  
  3322.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_history'
  3323.  
  3324.     raiserror('Creating clustered index ucMSlogreader_history', 0,1)
  3325.     CREATE UNIQUE CLUSTERED INDEX ucMSlogreader_history ON dbo.MSlogreader_history
  3326.         (agent_id, timestamp, runstatus, start_time, time) 
  3327.  
  3328.     if not exists (select * from sysindexes where name = 'nc1MSlogreader_history')
  3329.     begin
  3330.         raiserror('Creating nonclustered index nc1MSlogreader_history', 0,1)
  3331.         create nonclustered index nc1MSlogreader_history on MSlogreader_history(agent_id, start_time) 
  3332.     end            
  3333.    END
  3334.  
  3335.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_history' and type = 'U')
  3336.    BEGIN
  3337.         /****************************************************************************/
  3338.           raiserror('Creating table MSdistribution_history', 0,1)
  3339.         /****************************************************************************/    
  3340.  
  3341.         CREATE TABLE dbo.MSdistribution_history
  3342.         (
  3343.         agent_id int NOT NULL,
  3344.         runstatus int NOT NULL,
  3345.         start_time datetime NOT NULL,
  3346.         time datetime NOT NULL,
  3347.         duration int NOT NULL,
  3348.         comments nvarchar(255) NOT NULL,
  3349.         xact_seqno varbinary(16) NULL,
  3350.  
  3351.         -- Current statistics
  3352.         current_delivery_rate float NOT NULL,
  3353.         current_delivery_latency int NOT NULL,
  3354.  
  3355.         -- Session summary statistics
  3356.         delivered_transactions int NOT NULL,
  3357.         delivered_commands int NOT NULL,
  3358.         average_commands int NOT NULL,
  3359.         delivery_rate float NOT NULL,       
  3360.         delivery_latency int NOT NULL, 
  3361.  
  3362.         -- Summary statistics across all sessions
  3363.         total_delivered_commands int NOT NULL, 
  3364.  
  3365.         error_id int NOT NULL,
  3366.         updateable_row bit NOT NULL,
  3367.         timestamp NOT NULL
  3368.         )
  3369.  
  3370.     EXEC dbo.sp_MS_marksystemobject 'MSdistribution_history'
  3371.  
  3372.     raiserror('Creating clustered index ucMSdistribution_history', 0,1)
  3373.     CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_history ON dbo.MSdistribution_history
  3374.         (agent_id, timestamp, runstatus, start_time, time) 
  3375.  
  3376.     if not exists (select * from sysindexes where name = 'nc1MSdistribution_history')
  3377.     begin
  3378.         raiserror('Creating clustered index nc1MSdistribution_history', 0,1)
  3379.         create nonclustered index nc1MSdistribution_history on MSdistribution_history(agent_id, start_time) 
  3380.     end    
  3381.  
  3382.    END
  3383.  
  3384.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_history' and type = 'U')
  3385.    BEGIN
  3386.         /****************************************************************************/
  3387.       raiserror('Creating table MSmerge_history', 0,1)
  3388.         /****************************************************************************/    
  3389.  
  3390.         CREATE TABLE dbo.MSmerge_history
  3391.         (
  3392.         agent_id                    int             NOT NULL,
  3393.         runstatus               int             NOT NULL,
  3394.         start_time              datetime        NOT NULL,
  3395.         time                    datetime        NOT NULL,
  3396.         duration                int             NOT NULL,
  3397.         comments                nvarchar(255)   NOT NULL,
  3398.         delivery_time               int         NOT NULL,
  3399.         delivery_rate               float       NOT NULL,
  3400.         publisher_insertcount       int         NULL,
  3401.         publisher_updatecount       int         NULL,
  3402.         publisher_deletecount       int         NULL,
  3403.         publisher_conflictcount     int         NULL,
  3404.         subscriber_insertcount      int         NULL,
  3405.         subscriber_updatecount      int         NULL,
  3406.         subscriber_deletecount      int         NULL,
  3407.         subscriber_conflictcount    int         NULL,
  3408.         error_id                    int         NOT NULL,
  3409.         timestamp                               NOT NULL,
  3410.         updateable_row                bit            NOT NULL
  3411.         )        
  3412.  
  3413.     EXEC dbo.sp_MS_marksystemobject 'MSmerge_history'
  3414.  
  3415.     raiserror('Creating clustered index ucMSmerge_history', 0,1)
  3416.     CREATE UNIQUE CLUSTERED INDEX ucMSmerge_history ON dbo.MSmerge_history
  3417.         (agent_id, timestamp, runstatus, start_time, time) 
  3418.  
  3419.     if not exists (select * from sysindexes where name = 'nc1MSmerge_history')
  3420.         begin
  3421.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3422.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, start_time) 
  3423.         end
  3424.    END
  3425.    ELSE
  3426.    BEGIN
  3427.       IF NOT EXISTS (select * from syscolumns
  3428.         where name = 'updateable_row'
  3429.         and id=object_id('MSmerge_history'))
  3430.       BEGIN
  3431.          
  3432.         ALTER TABLE MSmerge_history ADD updateable_row bit default 1 NOT NULL
  3433.         if not exists (select * from sysindexes where name = 'nc1MSmerge_history')
  3434.         begin
  3435.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3436.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, start_time) 
  3437.         end
  3438.       END
  3439.    END
  3440.  
  3441.  
  3442.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_errors' and type = 'U')
  3443.    BEGIN
  3444.         /****************************************************************************/
  3445.         raiserror('Creating table MSrepl_errors', 0,1)
  3446.         /****************************************************************************/    
  3447.  
  3448.         CREATE TABLE dbo.MSrepl_errors
  3449.         (
  3450.         id                  int NOT NULL,
  3451.         time                datetime NOT NULL,
  3452.         error_type_id       int NULL,
  3453.         source_type_id      int NULL,
  3454.         source_name         nvarchar(100) NULL,
  3455.         error_code          sysname NULL,
  3456.         error_text          ntext NULL
  3457.         )
  3458.  
  3459.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_errors'
  3460.  
  3461.     raiserror('Creating clustered index ucMSrepl_errors', 0,1)
  3462.     CREATE CLUSTERED INDEX ucMSrepl_errors ON dbo.MSrepl_errors
  3463.         (id, time) -- WITH ALLOW_DUP_ROW
  3464.    END
  3465.  
  3466.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_agents' and type = 'U')
  3467.    BEGIN
  3468.         /****************************************************************************/
  3469.         raiserror('Creating table MSsnapshot_agents', 0,1)
  3470.         /****************************************************************************/    
  3471.  
  3472.         CREATE TABLE dbo.MSsnapshot_agents
  3473.         (
  3474.         id                  int IDENTITY NOT NULL,
  3475.         name                nvarchar(100) NOT NULL,
  3476.         publisher_id        smallint NOT NULL,
  3477.         publisher_db        sysname NOT NULL,
  3478.         publication         sysname NOT NULL,
  3479.         publication_type    int NOT NULL,       -- 0 transactional 1 snapshot 2 merge
  3480.         local_job           bit NOT NULL,
  3481.         job_id              binary(16) NULL,
  3482.         profile_id  int     NOT NULL
  3483.         )
  3484.  
  3485.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_agents'
  3486.  
  3487.     raiserror('Creating clustered index ucMSsnapshot_agents', 0,1)
  3488.     CREATE CLUSTERED INDEX ucMSsnapshot_agents ON dbo.MSsnapshot_agents
  3489.         (publication, publisher_db, publisher_id) 
  3490.  
  3491.     raiserror('Creatingindex iMSsnapshot_agents', 0,1)
  3492.     CREATE UNIQUE INDEX iMSsnapshot_agents ON dbo.MSsnapshot_agents
  3493.         (id)
  3494.    END
  3495.  
  3496.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_agents' and type = 'U')
  3497.    BEGIN
  3498.         /****************************************************************************/
  3499.         raiserror('Creating table MSlogreader_agents', 0,1)
  3500.         /****************************************************************************/    
  3501.  
  3502.         CREATE TABLE dbo.MSlogreader_agents
  3503.         (
  3504.         id                  int IDENTITY NOT NULL,
  3505.         name                nvarchar(100) NOT NULL,
  3506.         publisher_id        smallint NOT NULL,
  3507.         publisher_db        sysname NOT NULL,
  3508.         publication         sysname NOT NULL,-- Not used for SQL Server publisher
  3509.         local_job           bit NOT NULL,
  3510.         job_id              binary(16) NULL,
  3511.         profile_id  int     NOT NULL
  3512.         )
  3513.  
  3514.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_agents'
  3515.  
  3516.     raiserror('Creating clustered index ucMSlogreader_agents', 0,1)
  3517.     CREATE CLUSTERED INDEX ucMSlogreader_agents ON dbo.MSlogreader_agents
  3518.         (publisher_db, publisher_id) 
  3519.  
  3520.     raiserror('Creatingindex iMSlogreader_agents', 0,1)
  3521.     CREATE UNIQUE INDEX iMSlogreader_agents ON dbo.MSlogreader_agents
  3522.         (id)
  3523.    END
  3524.  
  3525.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_agents' and type = 'U')
  3526.    BEGIN
  3527.         /****************************************************************************/
  3528.         raiserror('Creating table MSdistribution_agents', 0,1)
  3529.         /****************************************************************************/    
  3530.  
  3531.         CREATE TABLE dbo.MSdistribution_agents
  3532.         (
  3533.         id                  int IDENTITY NOT NULL,
  3534.         name                nvarchar(100) NOT NULL,
  3535.         publisher_database_id int NOT NULL,
  3536.         publisher_id        smallint NOT NULL,
  3537.         publisher_db        sysname NOT NULL,
  3538.         publication         sysname NOT NULL,
  3539.         subscriber_id       smallint NULL, 
  3540.         subscriber_db       sysname NULL,
  3541.         subscription_type   int NOT NULL,
  3542.         local_job           bit NULL,
  3543.         job_id              binary(16) NULL,
  3544.         subscription_guid   binary(16) NOT NULL,
  3545.         profile_id  int     NOT NULL,
  3546.         anonymous_subid     uniqueidentifier NULL,
  3547.         subscriber_name     sysname NULL,
  3548.         virtual_agent_id    int NULL,
  3549.         anonymous_agent_id  int NULL,
  3550.         creation_date        datetime default (getdate()) not NULL
  3551.         )
  3552.  
  3553.         EXEC dbo.sp_MS_marksystemobject 'MSdistribution_agents'
  3554.  
  3555.         raiserror('Creating clustered index ucMSdistribution_agents', 0,1)
  3556.         CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_agents ON dbo.MSdistribution_agents
  3557.             (id)
  3558.  
  3559.         raiserror('Creatingindex iMSdistribution_agents', 0,1)
  3560.         CREATE INDEX iMSdistribution_agents ON dbo.MSdistribution_agents
  3561.             (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  3562.    END
  3563.  
  3564.     if not exists (select * from syscolumns where 
  3565.         id = object_id('MSdistribution_agents') and
  3566.         name = 'creation_date')
  3567.     begin
  3568.         alter table MSdistribution_agents add creation_date datetime default (getdate()) not null
  3569.     end 
  3570.  
  3571.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_agents' and type = 'U')
  3572.    BEGIN
  3573.         /****************************************************************************/
  3574.         raiserror('Creating table MSmerge_agents', 0,1)
  3575.         /****************************************************************************/    
  3576.  
  3577.         CREATE TABLE dbo.MSmerge_agents
  3578.         (
  3579.         id                  int IDENTITY NOT NULL,
  3580.         name                nvarchar(100) NOT NULL,
  3581.         publisher_id        smallint NOT NULL,
  3582.         publisher_db        sysname NOT NULL,
  3583.         publication         sysname NOT NULL,
  3584.         subscriber_id       smallint NULL,
  3585.         subscriber_db       sysname NULL,
  3586.         local_job           bit NULL,
  3587.         job_id              binary(16) NULL,
  3588.         profile_id          int NULL,
  3589.         anonymous_subid     uniqueidentifier NULL,
  3590.         subscriber_name     sysname NULL,
  3591.         creation_date        datetime default (getdate()) not NULL
  3592.         )
  3593.  
  3594.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_agents'
  3595.  
  3596.     raiserror('Creating clustered index ucMSmerge_agents', 0,1)
  3597.     CREATE CLUSTERED INDEX ucMSmerge_agents ON dbo.MSmerge_agents
  3598.         (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  3599.     
  3600.     raiserror('Creatingindex iMSmerge_agents', 0,1)
  3601.     CREATE UNIQUE INDEX iMSmerge_agents ON dbo.MSmerge_agents
  3602.         (id)
  3603.     END
  3604.  
  3605.     if not exists (select * from syscolumns where 
  3606.         id = object_id('MSmerge_agents') and
  3607.         name = 'creation_date')
  3608.     begin
  3609.         alter table MSmerge_agents add creation_date datetime default (getdate()) not null
  3610.     end 
  3611.  
  3612.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublication_access' and type = 'U')
  3613.     BEGIN
  3614.         /****************************************************************************/
  3615.         raiserror('Creating table MSpublication_access', 0,1)
  3616.         /****************************************************************************/    
  3617.  
  3618.         CREATE TABLE dbo.MSpublication_access
  3619.         (
  3620.         publication_id      int NULL,       -- Publication_id is unique in distribution database.
  3621.         login               sysname NOT NULL
  3622.                                             -- Logins in the publication access list, they nust
  3623.                                             -- exist at both publisher and distributor side.            
  3624.         )
  3625.  
  3626.         EXEC dbo.sp_MS_marksystemobject 'MSpublication_access'
  3627.  
  3628.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  3629.         CREATE UNIQUE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  3630.             (publication_id, login) 
  3631.     END
  3632.  
  3633.     -- For b3 upgrade
  3634.     if NOT EXISTS (select * from syscolumns where name='retention' and id=object_id('MSpublications'))
  3635.     begin
  3636.         alter table MSpublications add retention int NULL 
  3637.         UPDATE msdb..MSdistributiondbs set max_distretention=72 where
  3638.             name = db_name()
  3639.     end
  3640.  
  3641.     -- drop default_access column
  3642.     if exists (select * from syscolumns where id = object_id('MSpublications') and
  3643.         name = 'default_access')
  3644.     begin
  3645.         alter table MSpublications drop column default_access
  3646.     end 
  3647.  
  3648.     -- Drop publisher_id column
  3649.     if EXISTS (select * from syscolumns where name='publisher_id' and id=object_id('MSpublication_access'))
  3650.     begin
  3651.         drop index MSpublication_access.ucMSpublication_access
  3652.  
  3653.         alter table MSpublication_access drop column publisher_id
  3654.  
  3655.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  3656.         CREATE UNIQUE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  3657.         (publication_id, login) 
  3658.     end
  3659.  
  3660.  
  3661. GO
  3662.  
  3663. dump tran master with no_log
  3664. go
  3665.  
  3666. raiserror('Creating procedure sp_MSadd_distributor_alerts_and_responses', 0,1)
  3667. go
  3668. create proc sp_MSadd_distributor_alerts_and_responses
  3669. as
  3670.  
  3671.     declare @description    nvarchar(500)
  3672.     declare @category_name  sysname
  3673.     declare @agent_name     sysname
  3674.     declare @response_job   nvarchar(100)
  3675.     declare @alert_name     nvarchar(100)
  3676.     declare @alert_id       int
  3677.     declare @retcode        int
  3678.  
  3679.     --
  3680.     -- Add replication alert response jobs
  3681.     --
  3682.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 18
  3683.  
  3684.     -- Add Validation failure response job
  3685.     -- (20570,10,0,'Reinitialize Subscriptions On Validation Failure.', 1033)   -- Title of a alert response job
  3686.     -- (20571,10,0,'Reiniitializes all subscriptions that have failed due to data validation failures.', 1033)  -- Description of a alert response job
  3687.     set @response_job = formatmessage(20570)
  3688.     set @description = formatmessage(20571)
  3689.     
  3690.     -- Delete the job if it already exists
  3691.     if (exists (select * from msdb..sysjobs_view where name = @response_job))
  3692.     begin
  3693.         exec @retcode = msdb.dbo.sp_delete_job @job_name = @response_job
  3694.         if @@ERROR <> 0 or @retcode <> 0
  3695.             return (1)
  3696.     end
  3697.  
  3698.     execute @retcode = msdb.dbo.sp_MSadd_repl_job 
  3699.         @response_job,
  3700.         @subsystem = 'TSQL',
  3701.         @server = @@SERVERNAME,
  3702.         @databasename = 'master',
  3703.         @description = @description,
  3704.         @command = 'exec dbo.sp_MSreinit_failed_subscriptions @failure_level = 1',
  3705.         @enabled = 1,
  3706.         @loghistcompletionlevel = 0,
  3707.         @retryattempts = 0,
  3708.         @category_name = @category_name
  3709.     if @@ERROR <> 0 or @retcode <> 0
  3710.         return (1)
  3711.  
  3712.     --
  3713.     -- Add replication alerts
  3714.     --
  3715.  
  3716.     -- Get Replication category name (assumes category_id = 20)
  3717.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 20
  3718.  
  3719.     -- Add Failure Alert
  3720.     set @alert_name = formatmessage(20536)  
  3721.     set @alert_id = 14151 -- corresponding to formatmessage(20536),  Failure alert
  3722.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3723.     begin
  3724.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14151
  3725.         if @@error <> 0 or @retcode <> 0
  3726.             return 1
  3727.     end
  3728.  
  3729.     -- Add Retry Alert
  3730.     set @alert_name=formatmessage(20537)
  3731.     set @alert_id = 14152 -- corresponding to formatmessage(20537),  Retry alert
  3732.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3733.     begin
  3734.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14152
  3735.         if @@error <> 0 or @retcode <> 0
  3736.             return 1
  3737.     end
  3738.  
  3739.     -- Add Success Alert
  3740.     set @alert_name=formatmessage(20540)
  3741.     set @alert_id = 14150 -- corresponding to formatmessage(20540), 
  3742.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3743.     begin
  3744.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14150
  3745.         if @@error <> 0 or @retcode <> 0
  3746.             return 1
  3747.     end
  3748.  
  3749.     -- Add Shutdown request Alert
  3750.     set @alert_name=formatmessage(20578)
  3751.     set @alert_id = 20578 --  Custom agent shutdown message
  3752.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3753.     begin
  3754.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20578
  3755.         if @@error <> 0 or @retcode <> 0
  3756.             return 1
  3757.     end
  3758.  
  3759.     -- Add Validation Failure Alert
  3760.     set @alert_name=formatmessage(20565)
  3761.     set @alert_id = 20574 -- corresponding to formatmessage(20574), 
  3762.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3763.     begin
  3764.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20574
  3765.         if @@error <> 0 or @retcode <> 0
  3766.             return 1
  3767.     end
  3768.  
  3769.     -- Add Validation Success Alert
  3770.     set @alert_name=formatmessage(20566)
  3771.     set @alert_id = 20575 -- corresponding to formatmessage(20575), 
  3772.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3773.     begin
  3774.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20575
  3775.         if @@error <> 0 or @retcode <> 0
  3776.             return 1
  3777.     end
  3778.  
  3779.     -- Add Reinitialized after Validation Failure
  3780.     set @alert_name=formatmessage(20573)
  3781.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  3782.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  3783.     begin
  3784.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20572
  3785.         if @@error <> 0 or @retcode <> 0
  3786.             return 1
  3787.     end
  3788.  
  3789. GO
  3790.  
  3791.  
  3792. raiserror('Creating procedure sp_MSdrop_distributor_alerts_and_responses', 0,1)
  3793. go
  3794. create proc sp_MSdrop_distributor_alerts_and_responses
  3795. as
  3796.  
  3797.     declare @name           nvarchar(100)
  3798.     declare @alert_id       int
  3799.     declare @retcode        int
  3800.  
  3801.     --
  3802.     -- Delete alerts and response jobs
  3803.     --
  3804.  
  3805.     -- Drop Replication Checkup Agent
  3806.     select @name = formatmessage(20533)
  3807.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name and
  3808.         originating_server = '(local)')
  3809.     BEGIN
  3810.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  3811.         IF @@ERROR <> 0 or @retcode <> 0
  3812.         BEGIN
  3813.             return (1)            
  3814.         END
  3815.     END
  3816.  
  3817.     -- Drop Reinit subscription response job
  3818.     set @name = formatmessage(20570)
  3819.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name and
  3820.         originating_server = '(local)')
  3821.     BEGIN
  3822.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  3823.         IF @@ERROR <> 0 or @retcode <> 0
  3824.         BEGIN
  3825.             return (1)            
  3826.         END
  3827.     END
  3828.  
  3829.     -- Drop the alerts
  3830.     set @alert_id = 14150 -- success alert
  3831.     set @name=formatmessage(20540)
  3832.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3833.     begin
  3834.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3835.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3836.         if @@error <> 0 or @retcode <> 0
  3837.             return (1)            
  3838.     end
  3839.     
  3840.     set @alert_id = 14151 -- failure alert
  3841.     set @name = formatmessage(20536)
  3842.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3843.     begin
  3844.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3845.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3846.         if @@error <> 0 or @retcode <> 0
  3847.             return (1)            
  3848.     end
  3849.     
  3850.     set @alert_id = 14152 -- retry alert
  3851.     set @name = formatmessage(20537)
  3852.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3853.     begin
  3854.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3855.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3856.         if @@error <> 0 or @retcode <> 0
  3857.             return (1)            
  3858.     end
  3859.  
  3860.     set @alert_id = 14153 -- warnning alert
  3861.     set @name = formatmessage(20540)
  3862.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3863.     begin
  3864.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3865.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3866.         if @@error <> 0 or @retcode <> 0
  3867.             return (1)            
  3868.     end
  3869.  
  3870.     -- Remove Validation Failure Alert
  3871.     set @alert_id = 20574 
  3872.     set @name = formatmessage(20565)
  3873.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3874.     begin
  3875.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3876.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3877.         if @@error <> 0 or @retcode <> 0
  3878.             return (1)            
  3879.     end
  3880.  
  3881.     -- Remove Validation Sucess Alert
  3882.     set @alert_id = 20575
  3883.     set @name = formatmessage(20566)
  3884.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3885.     begin
  3886.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3887.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3888.         if @@error <> 0 or @retcode <> 0
  3889.             return (1)            
  3890.     end
  3891.     
  3892.     -- Remove Reinitialized after Validation Failure
  3893.     set @alert_id = 20525 -- checksum alert
  3894.     set @name = formatmessage(20573)
  3895.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3896.     begin
  3897.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3898.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3899.         if @@error <> 0 or @retcode <> 0
  3900.             return (1)            
  3901.     end
  3902.  
  3903.     -- Remove subscription reinitialized after validation failure
  3904.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  3905.     set @name=formatmessage(20573)
  3906.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3907.     begin
  3908.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3909.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3910.         if @@error <> 0 or @retcode <> 0
  3911.             return (1)            
  3912.     end
  3913.  
  3914.     -- Remove Shutdown request Alert
  3915.     set @alert_id = 20578 --  Custom agent shutdown message
  3916.     set @name=formatmessage(20578)
  3917.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  3918.     begin
  3919.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  3920.         exec @retcode = msdb.dbo.sp_delete_alert @name
  3921.         if @@error <> 0 or @retcode <> 0
  3922.             return (1)            
  3923.     end
  3924.  
  3925. GO
  3926. raiserror('Creating procedure sp_adddistributor', 0,1)
  3927. go
  3928.  
  3929. CREATE PROCEDURE sp_adddistributor (
  3930.     @distributor sysname,            /* distributor server name */
  3931.     @heartbeat_interval int = 10,    -- minutes
  3932.     @password sysname = NULL
  3933.  
  3934. ) AS
  3935.  
  3936.     SET NOCOUNT ON
  3937.  
  3938.     /*
  3939.     ** Declarations.
  3940.     */
  3941.     DECLARE @retcode int
  3942.     DECLARE @agentname nvarchar(100)
  3943.     DECLARE @command nvarchar(255)
  3944.     DECLARE @distribution_db sysname
  3945.     DECLARE @distproc nvarchar(255)
  3946.     DECLARE @dist_rpcname sysname
  3947.     DECLARE @server_added bit
  3948.     declare @login sysname
  3949.  
  3950.     select @server_added = 0 
  3951.     select @login = 'distributor_admin'
  3952.  
  3953.     /* 
  3954.     ** Check if replication components are installed on this server
  3955.     */
  3956.     exec @retcode = dbo.sp_MS_replication_installed
  3957.     if (@retcode <> 1)
  3958.     begin
  3959.         return (1)
  3960.     end
  3961.  
  3962.     -- Must be at master db.
  3963.     IF db_name() <> 'master'
  3964.     BEGIN
  3965.         RAISERROR(5001, 16,-1)
  3966.         return (1)
  3967.     END
  3968.     /*
  3969.     ** Parameter Check:  @distributor.
  3970.     ** Check to make sure that the distributor is not NULL and that it
  3971.     ** conforms to the rules for identifiers.
  3972.     */
  3973.     IF @distributor IS NULL
  3974.         BEGIN
  3975.             RAISERROR (14043, 16, -1, '@distributor')
  3976.             RETURN (1)
  3977.         END
  3978.  
  3979.     EXECUTE @retcode = dbo.sp_validname @distributor
  3980.  
  3981.     IF @@ERROR <> 0 OR @retcode <> 0
  3982.        RETURN (1)
  3983.  
  3984.     IF @password = N''
  3985.         select @password = NULL
  3986.     
  3987.     -- Seed default password with random value only for a local distributor.
  3988.     IF (@password is null) AND (UPPER(@@SERVERNAME) = UPPER(@distributor))
  3989.         select @password = convert(sysname, newid())
  3990.     /*
  3991.     ** Check to make sure that the distributor doesn't already exist.
  3992.     */
  3993.     SELECT @dist_rpcname = NULL
  3994.     SELECT @dist_rpcname = srvname FROM master..sysservers
  3995.               WHERE  srvstatus & 8 <> 0
  3996.     IF @dist_rpcname IS NOT NULL
  3997.     BEGIN
  3998.         RAISERROR (14099, 16, -1, @dist_rpcname)
  3999.         RETURN(1)
  4000.     END
  4001.  
  4002.     -- drop repl_distributor if it exists.
  4003.     if exists (select * from master..sysservers where lower(srvname) = 
  4004.         'repl_distributor')
  4005.     begin
  4006.         exec @retcode = dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4007.         IF @@error <> 0 OR @retcode <> 0
  4008.         BEGIN
  4009.             RETURN(1)
  4010.         END
  4011.     end
  4012.  
  4013.     -- Add the linked server entry for the distributor
  4014.     -- Note we do this even for local server for consistancy
  4015.     EXECUTE @retcode = dbo.sp_addserver  'repl_distributor'
  4016.     IF @@error <> 0 OR @retcode <> 0
  4017.     BEGIN
  4018.         RETURN (1)
  4019.     END
  4020.  
  4021.     select @server_added = 1
  4022.  
  4023.     -- Mark system link
  4024.     EXECUTE @retcode = dbo.sp_serveroption  'repl_distributor', 'system','true'
  4025.     IF @@error <> 0 OR @retcode <> 0
  4026.     BEGIN
  4027.         RETURN (1)
  4028.     END
  4029.  
  4030.     EXECUTE @retcode = dbo.sp_setnetname  'repl_distributor', @distributor
  4031.     IF @@error <> 0 OR @retcode <> 0
  4032.     BEGIN
  4033.         goto UNDO
  4034.     END
  4035.         
  4036.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  4037.         @rmtsrvname= 'repl_distributor',
  4038.         @useself = 'false',
  4039.         @locallogin = NULL,
  4040.         @rmtuser = @login,
  4041.         @rmtpassword = @password
  4042.     IF @@error <> 0 OR @retcode <> 0
  4043.     BEGIN
  4044.         goto UNDO
  4045.     END
  4046.  
  4047.  
  4048.     /*
  4049.     ** If this is not the local server, remote distributor must be set up first
  4050.     */
  4051.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  4052.     BEGIN
  4053.         /*
  4054.         ** Test to see if the local server is defined as publisher
  4055.         ** at the remove distributor.
  4056.         ** Note: cannot call sp_helpdistributor locally since the server is not
  4057.         ** marked for distribution.
  4058.         ** We can not move the serveroption call before this RPC because RPC failure
  4059.         ** may cause the SP to terminate. Thus, we can not UNDO the server option.
  4060.         ** give all parameters to work around SQL SERVER 7.0 rpc named param bug.
  4061.         */
  4062.         SELECT @distproc = 'repl_distributor.master.dbo.sp_helpdistributor'
  4063.     
  4064.         DECLARE @loc_directory             nvarchar(255)
  4065.         DECLARE @loc_account             nvarchar(255)
  4066.         DECLARE @loc_mindistretention     int
  4067.         DECLARE @loc_maxdistretention     int
  4068.         DECLARE @loc_historyretention   int  
  4069.         DECLARE @loc_historycleanupagent nvarchar(100)
  4070.         DECLARE @loc_distribcleanupagent nvarchar(100)
  4071.         DECLARE @alert_name nvarchar(100)
  4072.         DECLARE @alert_id int
  4073.  
  4074.         EXECUTE @distproc
  4075.             @distributor OUTPUT,
  4076.             @distribution_db OUTPUT,
  4077.             @loc_directory OUTPUT,
  4078.             @loc_account OUTPUT,
  4079.             @loc_mindistretention OUTPUT,
  4080.             @loc_maxdistretention OUTPUT,
  4081.             @loc_historyretention OUTPUT,
  4082.             @loc_historycleanupagent OUTPUT,
  4083.             @loc_distribcleanupagent OUTPUT,
  4084.             @@SERVERNAME,
  4085.             @local = 'local'
  4086.  
  4087.         IF @@error <> 0 OR @retcode <> 0 OR @distribution_db is NULL
  4088.         BEGIN
  4089.             RAISERROR (21007,16,-1)
  4090.             GOTO UNDO
  4091.         END
  4092.  
  4093.         /* Activate the dist publisher at the remote distributor */
  4094.         SELECT @distproc = 'repl_distributor.master.dbo.sp_changedistpublisher'
  4095.             EXECUTE @retcode = @distproc @@SERVERNAME, 'active','true'
  4096.         IF @@error <> 0 OR @retcode <> 0
  4097.         BEGIN
  4098.             GOTO UNDO
  4099.         END
  4100.     END
  4101.     ELSE
  4102.     /* set the registry */
  4103.     BEGIN
  4104.         EXEC @retcode = dbo.sp_MScreate_distributor_tables 
  4105.         if @@error <> 0 or @retcode <> 0
  4106.             goto UNDO
  4107.  
  4108.         declare @distributor_login sysname
  4109.         select @distributor_login = 'distributor_admin'
  4110.  
  4111.         -- Add publisher rpc login
  4112.         if not exists (select * from master..syslogins where loginname = @distributor_login)
  4113.         begin
  4114.             EXEC @retcode = dbo.sp_addlogin @loginame = @distributor_login,
  4115.                 @passwd = @password
  4116.             if @@error <> 0 or @retcode <> 0
  4117.                 goto UNDO
  4118.         end
  4119.         else
  4120.         begin
  4121.             -- Change the password if the distributor is local
  4122.             EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  4123.             if @@error <> 0 or @retcode <> 0
  4124.                 goto UNDO
  4125.         end
  4126.  
  4127.         -- Add the login to sysadmin
  4128.         -- Refer to sp_MSpublication_access in distribution db
  4129.         if is_srvrolemember('sysadmin', @distributor_login) <> 1
  4130.         begin
  4131.             exec @retcode = dbo.sp_addsrvrolemember @distributor_login, 'sysadmin'
  4132.             IF @@error <> 0 OR @retcode <> 0
  4133.                 GOTO UNDO
  4134.         end
  4135.  
  4136.         -- Add Replication Agent Checkup Agent
  4137.         exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = @heartbeat_interval
  4138.         if @@error <> 0 or @retcode <> 0
  4139.             goto UNDO
  4140.         
  4141.         delete msdb..MSdistributor where property = 'heartbeat_interval'
  4142.         if @@error <> 0
  4143.             goto UNDO
  4144.         insert into msdb..MSdistributor (property, value) values ('heartbeat_interval',
  4145.             convert(nvarchar(10), @heartbeat_interval))
  4146.         if @@error <> 0 
  4147.             goto UNDO
  4148.  
  4149.         -- Add Replication Alerts and Response Jobs
  4150.         exec @retcode = dbo.sp_MSadd_distributor_alerts_and_responses
  4151.         if @@error <> 0 or @retcode <> 0
  4152.             goto UNDO
  4153.     
  4154.     END
  4155.  
  4156.  
  4157.     /*
  4158.     ** Set the server option to indicate that this is a distributor.
  4159.     ** 
  4160.     */
  4161.     EXECUTE @retcode = dbo.sp_serveroption 'repl_distributor', 'dist', true
  4162.     IF @@error <> 0 OR @retcode <> 0
  4163.     BEGIN
  4164.         GOTO UNDO
  4165.     END
  4166.  
  4167.     -- Set sp_MSrepl_startup to be a startup stored procedure
  4168.     -- Note: This needs to be after the marking the distribution server
  4169.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  4170.     if @@error <> 0 or @retcode <> 0
  4171.         goto UNDO1
  4172.  
  4173.     exec @retcode = dbo.sp_MSrepl_startup
  4174.     if @@error <> 0 or @retcode <> 0
  4175.         goto UNDO1
  4176.  
  4177.     RETURN (0)
  4178.  
  4179. UNDO1:
  4180.     exec dbo.sp_serveroption 'repl_distributor', 'dist', false
  4181.  
  4182. UNDO:
  4183.     IF @server_added = 1
  4184.     begin
  4185.         -- Drop the remote logins, otherwise, sp_dropserver will fail.
  4186.         EXECUTE dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4187.     end
  4188.     
  4189.     RETURN (1)
  4190. GO
  4191.  
  4192.  
  4193. raiserror('Creating procedure sp_changedistributor_property', 0,1)
  4194. go
  4195.  
  4196. CREATE PROCEDURE sp_changedistributor_property (
  4197.     @property sysname     = NULL,       /* The property to change */
  4198.     @value nvarchar(255)  = NULL        /* The new property value */
  4199.     ) AS
  4200.  
  4201.     declare @retcode int
  4202.     declare @new_heartbeat_interval int 
  4203.  
  4204.     -- Check to make sure the local server is a distributor
  4205.     if not exists (SELECT * FROM master..sysservers
  4206.               WHERE  srvstatus & 8 <> 0 and
  4207.               UPPER(datasource) = UPPER(@@servername))
  4208.     begin
  4209.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  4210.         return(1)
  4211.     end
  4212.  
  4213.     -- Return list of properties if @property is NULL
  4214.     if @property is NULL
  4215.     begin
  4216.         create table #tab1 (properties sysname NOT NULL)
  4217.         insert into #tab1 values ('heartbeat_interval')
  4218.         select * from #tab1
  4219.         return (0)
  4220.     end
  4221.  
  4222.     if @property is NULL
  4223.     begin
  4224.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4225.         begin
  4226.             select property from msdb..MSdistributor
  4227.             return 0
  4228.         end
  4229.         return 1
  4230.     end
  4231.  
  4232.     -- Check for valid property
  4233.     IF LOWER(@property) NOT IN ('heartbeat_interval')
  4234.     BEGIN
  4235.         RAISERROR (14154, 16, -1)
  4236.         RETURN (1)
  4237.     END
  4238.  
  4239.     if LOWER(@property) = 'heartbeat_interval'
  4240.     begin
  4241.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')  
  4242.         begin
  4243.             update msdb..MSdistributor set value = @value where property = @property
  4244.             if @@error <> 0
  4245.                 return 1
  4246.             select @new_heartbeat_interval = CONVERT(int, @value)
  4247.             exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = 
  4248.                 @new_heartbeat_interval
  4249.             if @@error <> 0 or @retcode <> 0
  4250.                 return 1
  4251.         end
  4252.         else 
  4253.             return 1
  4254.     end
  4255.  
  4256.     return (0)
  4257.  
  4258. GO
  4259.  
  4260. raiserror('Creating procedure sp_helpdistributor_properties', 0,1)
  4261. go
  4262.  
  4263. CREATE PROCEDURE sp_helpdistributor_properties 
  4264.      AS
  4265.  
  4266.     if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4267.     begin
  4268.         -- There is currently only one property, so this will work
  4269.         select 'heartbeat_interval' = convert(int, value) from msdb..MSdistributor where
  4270.             property = 'heartbeat_interval'
  4271.         return (0)
  4272.     end
  4273.     return (1)
  4274. GO
  4275.  
  4276. raiserror('Creating procedure sp_dropdistributor', 0,1)
  4277. go
  4278.  
  4279. CREATE PROCEDURE sp_dropdistributor
  4280. @no_checks bit = 0,
  4281. @ignore_distributor bit = 0
  4282. AS
  4283.  
  4284.     SET NOCOUNT ON
  4285.  
  4286.     /*
  4287.     ** Declarations.
  4288.     */
  4289.     DECLARE @retcode int
  4290.     DECLARE @distributor sysname
  4291.     DECLARE @agentname nvarchar(100)
  4292.     DECLARE @distbit int
  4293.     DECLARE @distribdb sysname
  4294.     DECLARE @foundSubscriber int
  4295.     DECLARE @proc nvarchar(255) 
  4296.     declare @optname sysname
  4297.     declare @name sysname
  4298.     DECLARE @transpublishdb_bit int
  4299.     DECLARE @mergepublishdb_bit int
  4300.     declare @job_name nvarchar(100)
  4301.     declare @alert_name nvarchar(100)
  4302.  
  4303.     declare @dist_rpcname sysname
  4304.  
  4305.     declare @alert_id   int
  4306.    
  4307.     SELECT @transpublishdb_bit = 1
  4308.     SELECT @mergepublishdb_bit = 4
  4309.     SELECT @foundSubscriber = 0
  4310.     SELECT @distbit = 16
  4311.  
  4312.     -- Get distributor name
  4313.     select @distributor = datasource, @dist_rpcname = srvname from master..sysservers 
  4314.         WHERE srvstatus & 8 <> 0
  4315.     if @distributor is null
  4316.     BEGIN
  4317.         RAISERROR (21043, 16, -1)
  4318.         RETURN(1)
  4319.     END
  4320.  
  4321.     if @no_checks = 1
  4322.     begin
  4323.         -- We are in bruteforce cleanup mode, drop everything.
  4324.         DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4325.                 SELECT name, N'publish' FROM master..sysdatabases      
  4326.                     WHERE (category & @transpublishdb_bit) <> 0 
  4327.                 UNION
  4328.                 select name, N'merge publish' from master..sysdatabases
  4329.                     WHERE (category & @mergepublishdb_bit) <> 0
  4330.                 FOR READ ONLY
  4331.  
  4332.         OPEN hCdropdistributor
  4333.         FETCH hCdropdistributor INTO @name, @optname
  4334.     
  4335.         WHILE (@@fetch_status <> -1)
  4336.         BEGIN
  4337.                         
  4338.             EXECUTE @retcode  = dbo.sp_replicationdboption 
  4339.               @dbname = @name,
  4340.               @optname = @optname,
  4341.               @value = 'false',
  4342.               @ignore_distributor = @ignore_distributor
  4343.             
  4344.             IF @@ERROR <> 0 OR @retcode <> 0
  4345.             BEGIN
  4346.                 CLOSE hCdropdistributor
  4347.                 DEALLOCATE hCdropdistributor
  4348.                 RETURN (1)
  4349.             END
  4350.             FETCH hCdropdistributor INTO @name, @optname
  4351.         end
  4352.  
  4353.         CLOSE hCdropdistributor
  4354.         DEALLOCATE hCdropdistributor
  4355.     
  4356.         -- Drop subscriber
  4357.         EXECUTE @retcode  = dbo.sp_dropsubscriber
  4358.           @subscriber = 'all',
  4359.           @ignore_distributor = @ignore_distributor
  4360.  
  4361.         IF @@ERROR <> 0 OR @retcode <> 0
  4362.             RETURN (1)
  4363.  
  4364.         IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  4365.         begin
  4366.             -- Clean up dist publishers
  4367.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4368.                     SELECT name FROM msdb..MSdistpublishers
  4369.             FOR READ ONLY
  4370.  
  4371.             OPEN hCdropdistributor
  4372.             FETCH hCdropdistributor INTO @name
  4373.  
  4374.             WHILE (@@fetch_status <> -1)
  4375.             BEGIN
  4376.                 exec @retcode = dbo.sp_dropdistpublisher
  4377.                     @publisher = @name,
  4378.                     @no_checks = 1
  4379.             
  4380.                 IF @@ERROR <> 0 OR @retcode <> 0
  4381.                 BEGIN
  4382.                     CLOSE hCdropdistributor
  4383.                     DEALLOCATE hCdropdistributor
  4384.                     RETURN (1)
  4385.                 END
  4386.                 FETCH hCdropdistributor INTO @name
  4387.             end
  4388.  
  4389.             CLOSE hCdropdistributor
  4390.             DEALLOCATE hCdropdistributor
  4391.  
  4392.             -- Clean up distribution dbs
  4393.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4394.                     SELECT name FROM msdb..MSdistributiondbs
  4395.             FOR READ ONLY
  4396.  
  4397.             OPEN hCdropdistributor
  4398.             FETCH hCdropdistributor INTO @name
  4399.     
  4400.             WHILE (@@fetch_status <> -1)
  4401.             BEGIN
  4402.                 exec @retcode = dbo.sp_dropdistributiondb
  4403.                     @database = @name
  4404.  
  4405.                 IF @@ERROR <> 0 OR @retcode <> 0
  4406.                 BEGIN
  4407.                     CLOSE hCdropdistributor
  4408.                     DEALLOCATE hCdropdistributor
  4409.                     RETURN (1)
  4410.                 END
  4411.                 FETCH hCdropdistributor INTO @name
  4412.             end
  4413.  
  4414.             CLOSE hCdropdistributor
  4415.             DEALLOCATE hCdropdistributor
  4416.         end
  4417.     end
  4418.     
  4419.     -- If everything should be cleaned up when we reach here with @no_checks = 1
  4420.     /*
  4421.     ** If local distributor, check if there are any distributor databases
  4422.     */
  4423.     IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  4424.     BEGIN
  4425.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs)
  4426.         BEGIN
  4427.             RAISERROR (14121, 16, -1, @distributor)
  4428.             RETURN(1)
  4429.         END
  4430.     END
  4431.     ELSE
  4432.     begin
  4433.         -- Check to see if there are database published.
  4434.         if exists (SELECT * FROM master..sysdatabases      
  4435.             WHERE (category & @transpublishdb_bit) <> 0 or 
  4436.             (category & @mergepublishdb_bit) <> 0)
  4437.         begin
  4438.             raiserror(21045, 16, -1)
  4439.             return(1)
  4440.         end
  4441.  
  4442.         -- Check to see if there are subscribers defined.
  4443.         if exists (SELECT *
  4444.                      FROM master..sysservers
  4445.                     WHERE srvstatus & 4 <> 0)
  4446.         begin
  4447.             raiserror(21008, 16, -1)
  4448.             return(1)
  4449.         end
  4450.     end
  4451.  
  4452.  
  4453.     /*
  4454.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  4455.     */
  4456.     if @ignore_distributor = 0 
  4457.     begin
  4458.         /*
  4459.         ** Get distribution server information
  4460.         */
  4461.         EXEC @retcode = dbo.sp_helpdistributor @publisher = @@SERVERNAME,
  4462.             @distribdb = @distribdb OUTPUT
  4463.         IF @@error <> 0 OR @retcode <> 0
  4464.         BEGIN
  4465.             RAISERROR (14071, 16, -1)
  4466.             RETURN (1)
  4467.         END
  4468.  
  4469.         IF @distribdb is NOT NULL
  4470.         BEGIN
  4471.             /* 
  4472.             ** Deactivate the dist publisher at the  distributor 
  4473.             ** Only do this if @distribdb is NOT NULL, which means the dist publisher
  4474.             ** if defined.
  4475.             */
  4476.             SELECT @proc = RTRIM(@dist_rpcname) + '.master.dbo.sp_changedistpublisher'
  4477.                 EXECUTE @retcode = @proc @@SERVERNAME, 'active','false'
  4478.             IF @@error <> 0 OR @retcode <> 0
  4479.             BEGIN
  4480.                 RETURN (1)
  4481.             END
  4482.         END
  4483.     end
  4484.  
  4485.  
  4486.     /*
  4487.     ** Clear the server option to indicate that this is a distributor.
  4488.     */
  4489.     EXECUTE @retcode = dbo.sp_serveroption @dist_rpcname, 'dist', false
  4490.     IF @@error <> 0 OR @retcode <> 0
  4491.         BEGIN
  4492.             RETURN(1)
  4493.         END
  4494.  
  4495.     -- Prevent dropping local server entry accidentally if user 
  4496.     -- set 'dist' server option on local server.
  4497.     if UPPER(@dist_rpcname) <> UPPER(@@servername)
  4498.     begin
  4499.         exec @retcode = dbo.sp_dropserver @dist_rpcname, 'droplogins'
  4500.         IF @@error <> 0 OR @retcode <> 0
  4501.         BEGIN
  4502.             RETURN(1)
  4503.         END
  4504.     end
  4505.  
  4506.  
  4507.     -- Bug 26775 drop table after unmark distributor to prevent 
  4508.     -- sp_helpdist* failures.
  4509.     /* If local, Drop replication category and alerts */
  4510.     IF UPPER(@distributor) = UPPER(@@SERVERNAME)
  4511.     BEGIN
  4512.  
  4513.         /* Cannot drop login, it might be in use by a system process 
  4514.         if exists (select * from master..syslogins where loginname = @distributor_login)
  4515.         begin
  4516.  
  4517.             execute @retcode = dbo.sp_droplogin 'distributor_admin'
  4518.             if @@error <> 0 or @retcode <> 0
  4519.                 RETURN (1)
  4520.         end
  4521.         */
  4522.  
  4523.         -- Drop Distributor Alerts and Jobs
  4524.         exec @retcode = dbo.sp_MSdrop_distributor_alerts_and_responses
  4525.         IF @@ERROR <> 0 or @retcode <> 0
  4526.         BEGIN
  4527.             return (1)            
  4528.         END
  4529.  
  4530.         -- Drop the two system tables.
  4531.         if exists (select * from msdb..sysobjects where name = 'MSdistpublishers'
  4532.             and xtype = 'U')
  4533.             drop table msdb..MSdistpublishers
  4534.     
  4535.         if @@error <> 0 
  4536.             return 1 ;
  4537.  
  4538.         if exists (select * from msdb..sysobjects where name = 'MSdistributiondbs'
  4539.             and xtype = 'U')
  4540.             drop table msdb..MSdistributiondbs
  4541.  
  4542.         if @@error <> 0
  4543.             return 1 ;
  4544.  
  4545.         if exists (select * from msdb..sysobjects where name = 'MSdistributor'
  4546.                 and xtype = 'U')
  4547.             drop table msdb..MSdistributor
  4548.  
  4549.         if @@error <> 0
  4550.             return 1 ;
  4551.  
  4552.         if exists (select * from msdb..sysobjects where name = 'sysreplicationalerts'
  4553.                 and xtype = 'U')
  4554.             drop table msdb..sysreplicationalerts
  4555.  
  4556.         if @@error <> 0
  4557.             return 1 ;
  4558.  
  4559.  
  4560.         if exists (select * from msdb..sysobjects where name = 'MSagent_profiles'
  4561.             and xtype = 'U')
  4562.             drop table msdb..MSagent_profiles
  4563.  
  4564.         if @@error <> 0
  4565.             return 1 ;
  4566.  
  4567.  
  4568.        if exists (select * from msdb..sysobjects where name = 'MSagent_parameters'
  4569.                 and xtype = 'U')
  4570.             drop table msdb..MSagent_parameters
  4571.  
  4572.         if @@error <> 0
  4573.             return 1 ;
  4574.  
  4575.     END
  4576.  
  4577.     -- Nnregister sp_MSrepl_startup as a startup stored procedure
  4578.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'false' 
  4579.     if @@error <> 0 or @retcode <> 0
  4580.         return 1
  4581.  
  4582.     RETURN (0)        
  4583. GO
  4584.  
  4585. dump tran master with NO_LOG
  4586. go
  4587.  
  4588. raiserror('Creating procedure sp_helpdistributiondb', 0,1)
  4589. go
  4590.  
  4591. CREATE PROCEDURE sp_helpdistributiondb (
  4592.     @database sysname = '%'
  4593.     ) AS
  4594.  
  4595.     SET NOCOUNT ON
  4596.  
  4597.     declare @retcode int
  4598.     declare @cmd    nvarchar(500)
  4599.  
  4600.     /*
  4601.     ** Check to make sure this is a distributor
  4602.     */
  4603.     IF NOT EXISTS (select * from master..sysservers
  4604.               where UPPER(datasource) = UPPER(@@SERVERNAME)
  4605.                  AND srvstatus & 8 <> 0)
  4606.     begin
  4607.     if @database <> '%' 
  4608.     begin
  4609.             raiserror (14114, 16, -1, @@SERVERNAME)
  4610.             return(1)
  4611.     end
  4612.     else
  4613.        return(0)
  4614.     end
  4615.  
  4616.     IF NOT EXISTS (select * from msdb..sysobjects where name = 'MSdistributiondbs' and type = 'U')
  4617.     begin
  4618.         raiserror (14071, 16, -1)
  4619.         return(1)
  4620.     end    
  4621.  
  4622.     /*
  4623.     ** Check if database is configured as a distributor database
  4624.     */
  4625.     IF @database <> '%' AND NOT EXISTS (select * from msdb..MSdistributiondbs where name = @database)
  4626.     begin
  4627.         raiserror (14117, 16, -1, @database)
  4628.         return(1)
  4629.     end    
  4630.  
  4631.     -- Begin from sp_helpdb
  4632.  
  4633.     /*  Create temp table before any DMP to enure dynamic
  4634.     **  Since we examine the status bits in sysdatabase and turn them
  4635.     **  into english, we need a temporary table to build the descriptions.
  4636.     */
  4637.     create table #spdbdesc
  4638.     (
  4639.         dbid    smallint    null,
  4640.         dbdesc  nvarchar(175)   null
  4641.     )
  4642.  
  4643.     /* 
  4644.     ** Since we need to execute dynamic SQL to get the list of files, create a temp 
  4645.     ** table to keep file descriptions
  4646.     */
  4647.     create table #spfiledesc
  4648.     (
  4649.         dbid    smallint        not null,
  4650.         fileid  smallint        not null,
  4651.         status  int        not null,
  4652.         size    int             not null,
  4653.         name    sysname         not null,
  4654.         filename nvarchar(260)   not null
  4655.     )   
  4656.  
  4657.     /*
  4658.     **  Initialize #spdbdesc from sysdatabases
  4659.     */
  4660.     insert into #spdbdesc (dbid)
  4661.             select dbid from master.dbo.sysdatabases d, msdb.dbo.MSdistributiondbs dist
  4662.                 where d.name like @database and
  4663.                 dist.name = d.name and 
  4664.                 (isnull(databaseproperty(d.name, N'issuspect'), 0) = 0 and isnull(databaseproperty(d.name, N'isshutdown'), 0) = 0)
  4665.  
  4666.     declare @name sysname
  4667.  
  4668.     /* 
  4669.     ** Insert the list of all files into #spfiledesc
  4670.     */
  4671.     declare c1 CURSOR LOCAL FAST_FORWARD FOR 
  4672.         select db_name (dbid) from #spdbdesc
  4673.     open c1
  4674.     fetch c1 into @name
  4675.     while @@fetch_status >= 0
  4676.     begin
  4677.  
  4678.         /* Insert row for each database */
  4679.         select @cmd =  ('insert into #spfiledesc
  4680.                 select db_id ('''+ @name + '''), fileid, status, size, RTRIM(name), RTRIM(filename) from' 
  4681.                 + ' ' + QUOTENAME(@name) + '.dbo.sysfiles')
  4682.         exec (@cmd)
  4683.         fetch c1 into @name
  4684.     end
  4685.     deallocate c1
  4686.  
  4687.     SELECT dist.name, min_distretention, max_distretention, history_retention,
  4688.         'history_cleanup_agent' =  formatmessage (20567, dist.name),
  4689.         'distributor_cleanup_agent' = formatmessage(20568, dist.name),
  4690.         'status'= 0, 
  4691.  
  4692.         -- Refer to sp_helpdb for the following query.
  4693.         'data_folder' = substring(v_data.filename, 1, len(v_data.filename) - charindex('\', reverse(v_data.filename))),
  4694.         'data_file' = right(v_data.filename, charindex('\', reverse(v_data.filename))-1),
  4695.         'data_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  4696.             where type = 'E'
  4697.                 and number = 1)
  4698.             * convert(dec(15), v_data.size)/ 1048576),
  4699.     
  4700.         'log_folder' = substring(v_log.filename, 1, len(v_log.filename) - charindex('\', reverse(v_log.filename))),
  4701.         'log_file' = right(v_log.filename, charindex('\', reverse(v_log.filename))-1),
  4702.         'log_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  4703.             where type = 'E'
  4704.                 and number = 1)
  4705.             * convert(dec(15), v_log.size)/ 1048576)
  4706.         
  4707.         FROM msdb.dbo.MSdistributiondbs dist,
  4708.         master.dbo.sysdatabases d, #spfiledesc v_data,
  4709.         #spfiledesc v_log
  4710.         WHERE dist.name LIKE @database
  4711.             
  4712.             and v_data.dbid = d.dbid 
  4713.             and v_data.dbid > 0 
  4714.             and v_data.status & 2 = 2
  4715.             and v_data.status & 0x40 = 0 -- data
  4716.             and v_data.fileid = (select min(v_data2.fileid) from #spfiledesc v_data2 where
  4717.                     v_data2.dbid = d.dbid 
  4718.                 and v_data2.dbid > 0    
  4719.                 and v_data2.status & 2 = 2
  4720.                 and v_data2.status & 0x40 = 0 ) -- data
  4721.  
  4722.             
  4723.             and v_log.dbid = d.dbid 
  4724.             and v_log.dbid > 0  
  4725.             and v_log.status & 2 = 2
  4726.             and v_log.status & 0x40 = 0x40 -- log
  4727.             and v_log.fileid = (select min(v_log2.fileid) from #spfiledesc v_log2 where
  4728.                     v_log2.dbid = d.dbid 
  4729.                 and v_log2.dbid > 0 
  4730.                 and v_log2.status & 2 = 2
  4731.                 and v_log2.status & 0x40 = 0x40 ) -- log
  4732.  
  4733.             and d.name = dist.name
  4734. GO
  4735.  
  4736.  
  4737. dump tran master with no_log
  4738. GO
  4739.  
  4740. raiserror('Creating procedure sp_changedistributiondb', 0,1)
  4741. go
  4742.  
  4743. CREATE PROCEDURE sp_changedistributiondb (
  4744.     @database sysname,
  4745.     @property sysname     = NULL,     /* The property to change */
  4746.     @value nvarchar(255)     = NULL      /* The new property value */
  4747.     ) AS
  4748.  
  4749.     SET NOCOUNT ON
  4750.  
  4751.     /*
  4752.     ** Declarations.
  4753.     */
  4754.     DECLARE @distributor sysname
  4755.     DECLARE @min_distretention     int
  4756.     DECLARE @max_distretention     int
  4757.     DECLARE @retcode             int
  4758.     DECLARE @new_min_distretention     int
  4759.     DECLARE @new_max_distretention     int
  4760.     DECLARE @new_history_retention     int
  4761.     DECLARE @agentname nvarchar(100)
  4762.     DECLARE @command nvarchar(255)
  4763.     DECLARE @security_mode int
  4764.     DECLARE @distbit int
  4765.  
  4766.     SELECT @distbit = 16
  4767.  
  4768.     /*
  4769.     ** Parameter Check:  @property.
  4770.     ** If the @property parameter is NULL, print the options.
  4771.     */
  4772.  
  4773.     IF @property IS NULL
  4774.         BEGIN
  4775.             CREATE TABLE #tab1 (properties sysname NOT NULL)
  4776.             INSERT INTO #tab1 VALUES ('min_distretention')
  4777.             INSERT INTO #tab1 VALUES ('max_distretention')
  4778.             INSERT INTO #tab1 VALUES ('history_retention')
  4779.             SELECT * FROM #tab1
  4780.             RETURN (0)
  4781.         END
  4782.  
  4783.     /*
  4784.     ** Parameter Check:  @property.
  4785.     ** Check to make sure that @property is a valid property in
  4786.     ** sysarticles.
  4787.     */
  4788.     IF @property IS NULL OR LOWER(@property) NOT IN ('min_distretention',
  4789.                                                      'max_distretention',
  4790.                                                      'history_retention')
  4791.              
  4792.         BEGIN
  4793.  
  4794.             RAISERROR (14115, 16, -1, 
  4795.             '''min_distretention'', ''max_distretention'' or ''history_retention''')
  4796.             RETURN (1)
  4797.         END
  4798.  
  4799.  
  4800.     /*
  4801.     ** Check to make sure this is a distributor
  4802.     */
  4803.     IF NOT EXISTS (SELECT * FROM master..sysservers
  4804.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME)
  4805.                  AND srvstatus & 8 <> 0)
  4806.     BEGIN
  4807.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  4808.         RETURN(1)
  4809.     END
  4810.  
  4811.     /*
  4812.     ** Check if database is configured as a distributor database
  4813.     */
  4814.     IF NOT EXISTS (SELECT * FROM master..sysdatabases
  4815.               WHERE name = @database
  4816.                  AND category & @distbit <> 0)
  4817.     BEGIN
  4818.         RAISERROR (14117, 16, -1, @database)
  4819.         RETURN(1)
  4820.     END
  4821.     
  4822.     /*
  4823.     ** Change the property.
  4824.     */
  4825.     IF LOWER(@property) = 'min_distretention'
  4826.         BEGIN
  4827.             IF @value IS NULL
  4828.                 BEGIN
  4829.                     RAISERROR (14043, 16, -1, '@value') 
  4830.                     RETURN (1)
  4831.                 END
  4832.  
  4833.             /*
  4834.             ** Set the MinDistRetention registry key value
  4835.             */
  4836.             SELECT @new_min_distretention = CONVERT(int, @value)
  4837.  
  4838.             /*
  4839.             ** Get MaxDistRetention value
  4840.             */
  4841.             SELECT @max_distretention = max_distretention FROM msdb..MSdistributiondbs
  4842.                 WHERE name = @database
  4843.                 
  4844.             /* 
  4845.             ** Check for invalid retention values 
  4846.             */
  4847.             IF @new_min_distretention < 0
  4848.                 BEGIN
  4849.                        RAISERROR(14106, 16, -1)
  4850.                     RETURN (1)
  4851.                 END
  4852.             IF @new_min_distretention > @max_distretention
  4853.                 BEGIN
  4854.                     RAISERROR(14107, 16, -1) 
  4855.                     RETURN (1)
  4856.                 END
  4857.  
  4858.             UPDATE msdb..MSdistributiondbs SET min_distretention = @new_min_distretention
  4859.                 WHERE name = @database
  4860.             IF @@error <> 0 
  4861.                 BEGIN
  4862.                     RETURN (1)
  4863.                 END
  4864.  
  4865.             /*
  4866.             ** Update Distribution Cleanup agent 
  4867.             */
  4868.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  4869.             j.job_id = s.job_id and
  4870.             j.category_id = 11 and
  4871.             s.database_name = @database
  4872.         
  4873.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  4874.                 CONVERT(nvarchar(12), @new_min_distretention) + ', @max_distretention = ' +
  4875.                 CONVERT(nvarchar(12), @max_distretention)
  4876.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  4877.                 @command = @command
  4878.             IF @@error <> 0 OR @retcode <> 0
  4879.                RETURN(1)
  4880.  
  4881.         END
  4882.  
  4883.     IF LOWER(@property) = 'max_distretention'
  4884.         BEGIN
  4885.             IF @value IS NULL
  4886.                 BEGIN
  4887.                     RAISERROR (14043, 16, -1, '@value') 
  4888.                     RETURN (1)
  4889.                 END
  4890.  
  4891.             /*
  4892.             ** Set the MaxDistRetention registry key value
  4893.             */
  4894.             SELECT @new_max_distretention = CONVERT(int, @value)
  4895.  
  4896.             /*
  4897.             ** Get MinDistRetention value
  4898.             */
  4899.             SELECT @min_distretention = min_distretention FROM msdb..MSdistributiondbs
  4900.                 WHERE name = @database
  4901.  
  4902.             /* 
  4903.             ** Check for invalid retention values 
  4904.             */
  4905.             IF @new_max_distretention < 0
  4906.                 BEGIN
  4907.                        RAISERROR(14106, 16, -1)
  4908.                     RETURN (1)
  4909.                 END
  4910.             IF @new_max_distretention < @min_distretention
  4911.                 BEGIN
  4912.                     RAISERROR(14107, 16, -1) 
  4913.                     RETURN (1)
  4914.                 END
  4915.  
  4916.             UPDATE msdb..MSdistributiondbs SET max_distretention = @new_max_distretention
  4917.                 WHERE name = @database
  4918.             IF @@error <> 0 
  4919.                 BEGIN
  4920.                     RETURN (1)
  4921.                 END
  4922.             /*
  4923.             ** Update Distribution Cleanup agent 
  4924.             */
  4925.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  4926.                 j.job_id = s.job_id and
  4927.                 j.category_id = 11 and
  4928.                 s.database_name = @database
  4929.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  4930.                 CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  4931.                 CONVERT(nvarchar(12), @new_max_distretention)
  4932.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  4933.                 @command = @command
  4934.             IF @@error <> 0 OR @retcode <> 0
  4935.                RETURN(1)
  4936.         END
  4937.  
  4938.     IF LOWER(@property) = 'history_retention'
  4939.         BEGIN
  4940.             IF @value IS NULL
  4941.                 BEGIN
  4942.                     RAISERROR (14043, 16, -1, '@value') 
  4943.                     RETURN (1)
  4944.                 END
  4945.  
  4946.             /*
  4947.             ** Set the HistoryRetention registry key value
  4948.             */
  4949.             SELECT @new_history_retention = CONVERT(int, @value)
  4950.  
  4951.             UPDATE msdb..MSdistributiondbs SET history_retention = @new_history_retention
  4952.                 WHERE name = @database
  4953.             IF @@error <> 0 
  4954.                 BEGIN
  4955.                     RETURN (1)
  4956.                 END
  4957.  
  4958.             /*
  4959.             ** Update History Cleanup agent 
  4960.             */
  4961.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  4962.                 j.job_id = s.job_id and
  4963.                 j.category_id = 12 and
  4964.                 s.database_name = @database
  4965.             SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  4966.                 CONVERT(nvarchar(12), @new_history_retention)
  4967.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  4968.                 @command = @command
  4969.             IF @@error <> 0 OR @retcode <> 0
  4970.                RETURN(1)
  4971.  
  4972.         END
  4973.  
  4974.  
  4975.  
  4976.  
  4977.     /*
  4978.     ** Return succeed.
  4979.     */
  4980.     RAISERROR (14105, 10, -1, @property)
  4981.     RETURN (0)
  4982. go
  4983.  
  4984. raiserror('Creating procedure sp_dropdistributiondb', 0,1)
  4985. go
  4986.  
  4987. CREATE PROCEDURE sp_dropdistributiondb (
  4988.     @database sysname        
  4989.     
  4990. ) AS
  4991.  
  4992.     SET NOCOUNT ON
  4993.     /*
  4994.     ** Declarations.
  4995.     */
  4996.     DECLARE @retcode int
  4997.     DECLARE @agentname nvarchar(100)
  4998.     DECLARE @reg_key nvarchar(255)
  4999.     DECLARE @security_mode int
  5000.     DECLARE    @distbit int
  5001.     DECLARE @distpublisher sysname
  5002.     DECLARE @distdb sysname
  5003.     DECLARE @device_name sysname
  5004.     DECLARE @command nvarchar(255)
  5005.  
  5006.     SELECT @distbit = 16
  5007.  
  5008.     /*
  5009.     ** Check to make sure this is a distributor
  5010.     */
  5011.     IF NOT EXISTS (SELECT * FROM master..sysservers
  5012.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME)
  5013.                  AND srvstatus & 8 <> 0)
  5014.     BEGIN
  5015.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5016.         RETURN(1)
  5017.     END
  5018.  
  5019.     /*
  5020.     ** Check if database is configured as a distributor database
  5021.     */
  5022.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database)
  5023.     BEGIN
  5024.         RAISERROR (14117, 16, -1, @database)
  5025.         RETURN(1)
  5026.     END
  5027.  
  5028.     /*
  5029.     ** Check if any DistPublishers are using this database
  5030.     */
  5031.     IF EXISTS (SELECT * FROM msdb..MSdistpublishers WHERE
  5032.         distribution_db = @database)
  5033.     BEGIN
  5034.         RAISERROR (14120, 16, -1, @database)
  5035.         RETURN (1)
  5036.     END
  5037.     
  5038.     /*
  5039.     ** Check if the DB is being currently used
  5040.     */
  5041.     IF EXISTS (SELECT * FROM master..sysprocesses WHERE
  5042.         dbid = DB_ID(@database))
  5043.     BEGIN
  5044.         RAISERROR (21122, 16, 1, @database)
  5045.         RETURN (1)
  5046.     END
  5047.        
  5048.  
  5049.     /*
  5050.     ** Drop the history cleanup agent.
  5051.     */
  5052.     -- Get agent name
  5053.     select @agentname = null
  5054.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5055.         j.job_id = s.job_id and
  5056.         j.category_id = 12 and
  5057.         s.database_name = @database
  5058.  
  5059.     if @agentname is not null
  5060.     BEGIN
  5061.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5062.             @job_name = @agentname
  5063.         IF @@ERROR <> 0 or @retcode <> 0
  5064.         BEGIN
  5065.             RETURN(1)
  5066.         END
  5067.     END
  5068.  
  5069.     /*
  5070.     ** Drop the distribution cleanup agent.
  5071.     */
  5072.     select @agentname = null
  5073.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5074.                 j.job_id = s.job_id and
  5075.                 j.category_id = 11 and
  5076.                 s.database_name = @database
  5077.     if @agentname is not null
  5078.     BEGIN
  5079.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5080.             @job_name = @agentname
  5081.         IF @@ERROR <> 0 or @retcode <> 0
  5082.         BEGIN
  5083.             RETURN(1)
  5084.         END
  5085.     END
  5086.  
  5087.     /*
  5088.     ** Update sysdatabase category bit before dropping the database
  5089.     ** Otherwise, the database can not be dropped.
  5090.     **
  5091.     */
  5092.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database
  5093.     IF @@ERROR <> 0
  5094.         BEGIN
  5095.             RETURN (1)
  5096.         END
  5097.  
  5098.  
  5099.     -- Drop the distributor db if it exists
  5100.     IF EXISTS (SELECT * FROM master..sysdatabases WHERE name = @database)
  5101.     BEGIN
  5102.  
  5103.         CREATE TABLE #db_existed
  5104.         (
  5105.         db_existed bit  NOT NULL
  5106.         )
  5107.  
  5108.         /* Get version stamp */
  5109.         SELECT @command = 'INSERT INTO #db_existed SELECT db_existed FROM ' +
  5110.             QUOTENAME(@database) + '..MSrepl_version'
  5111.     
  5112.         EXEC(@command)
  5113.         IF @@ERROR <> 0
  5114.         BEGIN
  5115.             RETURN(1)       
  5116.         END
  5117.  
  5118.         -- Drop the distribution db only if it is created in sp_adddistributiondb.
  5119.         IF NOT EXISTS (SELECT * FROM #db_existed where db_existed = 0x1)
  5120.         BEGIN
  5121.  
  5122.             -- we're dropping the database, so don't bother to clean up
  5123.             -- individual records.  Just make sure we blow away the synchronization
  5124.             -- files & directories hosted by the file system.
  5125.  
  5126.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSdrop_snapshot_dirs'
  5127.             SELECT @command = 'EXEC ' + @command
  5128.             EXEC (@command)
  5129.             IF (@@ERROR <> 0)
  5130.             BEGIN
  5131.                 RETURN (1)
  5132.             END
  5133.     
  5134.             /* Save the device info before dropping the database */
  5135.             CREATE TABLE #distdbdevices (name sysname  NOT NULL)
  5136.  
  5137.             /* Drop distributor devices */
  5138.             /* Query is copied from sp_helpdb */
  5139.             SELECT @command = 
  5140.                 'INSERT INTO #distdbdevices select DISTINCT name from ' + QUOTENAME(@database) + '.dbo.sysfiles'
  5141.             EXEC (@command)
  5142.             IF @@ERROR <>0
  5143.             BEGIN
  5144.                 RETURN(1)      
  5145.             END
  5146.  
  5147.             /*
  5148.             ** Drop the distribution database
  5149.             */
  5150.             SELECT @command = 'drop database ' + QUOTENAME(@database)
  5151.             EXEC (@command)
  5152.             IF @@ERROR <> 0
  5153.                 BEGIN
  5154.                     /* Mark the database as distribution database again 
  5155.                     ** Otherwise, this sp will fail when it is reentered.
  5156.                     ** It is often the case that if there's an open session
  5157.                     ** on the database, it can not be dropped.
  5158.                     */
  5159.                     RETURN(1)
  5160.                 END
  5161.  
  5162.             /* Drop distributor devices */
  5163.             /* Query is copied from sp_helpdb */
  5164.             /* SQL SERVER 7.0 may drop some auto generated device files, drop drop them here
  5165.                again
  5166.             */
  5167.             DECLARE hCdropdistributiondb CURSOR LOCAL FAST_FORWARD FOR
  5168.                 SELECT DISTINCT dd.name FROM #distdbdevices dd, master.dbo.sysdevices ss
  5169.                     WHERE ss.name = dd.name
  5170.             FOR READ ONLY
  5171.     
  5172.             OPEN hCdropdistributiondb
  5173.             FETCH hCdropdistributiondb INTO @device_name
  5174.  
  5175.             WHILE (@@fetch_status <> -1)
  5176.             BEGIN
  5177.                 /* 
  5178.                 ** Device may be used by other databases
  5179.                 ** Ignore all errors 
  5180.                 */
  5181.                 EXEC dbo.sp_dropdevice @device_name, DELFILE
  5182.                 FETCH hCdropdistributiondb INTO @device_name
  5183.             END
  5184.  
  5185.             CLOSE hCdropdistributiondb
  5186.             DEALLOCATE hCdropdistributiondb
  5187.  
  5188.         END
  5189.         -- else, database did exist before it was made the distributor, 
  5190.         -- do full cleanup
  5191.         ELSE
  5192.         BEGIN
  5193.  
  5194.             -- run 'fast clean' routine first. this removes repldata files
  5195.             -- and truncates MSrepl_transactions/commands
  5196.  
  5197.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSfast_delete_trans'
  5198.             SELECT @command = 'EXEC ' + @command
  5199.             EXEC (@command)
  5200.             IF (@@ERROR <> 0)
  5201.             BEGIN
  5202.                 RETURN (1)
  5203.             END
  5204.  
  5205.             -- Do distribution cleanup the final time
  5206.             -- this handles those fiddly agent tables & such
  5207.             -- ( and would delete commands/xacts if we hadn't already purged 'em )
  5208.  
  5209.             SELECT @command = QUOTENAME(@database) + 
  5210.                 '.dbo.sp_MSdistribution_cleanup @min_distretention = 0, @max_distretention = 0'
  5211.             SELECT @command = 'EXEC ' + @command
  5212.             EXEC (@command)
  5213.             IF (@@ERROR <> 0)
  5214.             BEGIN
  5215.                 RETURN (1)
  5216.             END
  5217.         END
  5218.     END
  5219.     
  5220.  
  5221.     /*
  5222.     ** Delete the DistributionDB entry
  5223.     */
  5224.  
  5225.     DELETE msdb.dbo.MSdistributiondbs where name = @database
  5226.     IF @@error <> 0
  5227.     BEGIN
  5228.         RETURN(1)
  5229.     END
  5230.     
  5231.  
  5232.     RETURN (0)
  5233.                 
  5234. GO
  5235.  
  5236. dump tran master with no_log
  5237. GO
  5238. raiserror('Creating procedure sp_adddistributiondb', 0,1)
  5239. go
  5240.  
  5241. CREATE PROCEDURE sp_adddistributiondb (
  5242.     @database sysname,
  5243.     @data_folder nvarchar(255) = NULL,
  5244.     @data_file nvarchar(255) = NULL,            /* physical file name */
  5245.     @data_file_size int = 2,                    /* Default: 2MB */            
  5246.     @log_folder nvarchar(255) = NULL,
  5247.     @log_file nvarchar(255) = NULL,             /* physical file name */
  5248.     @log_file_size int = 0,
  5249.     @min_distretention int = 0,                 /* min distribution retention period in hours */
  5250.     @max_distretention int = 72,                /* max distribution retention period in hours */
  5251.     @history_retention int = 48,                /* history retention period in hours */
  5252.     @security_mode int = 0,                     /* distributor login security 0 standard 1 integrated */
  5253.     @login sysname = 'sa',                      /* standard login name */
  5254.     @password sysname = NULL,                   /* standard login password */
  5255.     @createmode int = 0  /* 0: use create db for attach (recommended), 
  5256.                             1: create db or use existing but no attach (this is the old way), 
  5257.                             2: create for instdist and detach only */
  5258.     ) AS
  5259.  
  5260.     SET NOCOUNT ON
  5261.  
  5262.     /*
  5263.     ** Declarations.
  5264.     */
  5265.     DECLARE @data_path nvarchar(512)
  5266.     DECLARE @log_path nvarchar(512)
  5267.     
  5268.     DECLARE @data_path_quoted_for_copy nvarchar(512)
  5269.     DECLARE @log_path_quoted_for_copy nvarchar(512)
  5270.  
  5271.     DECLARE @logical_data_file nvarchar(255)
  5272.     DECLARE @logical_log_file nvarchar(255)
  5273.     DECLARE @canneddbdata_file nvarchar(255)
  5274.     DECLARE @canneddblog_file nvarchar(255)
  5275.     DECLARE @filecopy_cmd nvarchar(255)
  5276.     DECLARE @file_exists int
  5277.     DECLARE @data_file_preexists int
  5278.     DECLARE @log_file_preexists int
  5279.     DECLARE @osql_cmd nvarchar(255)
  5280.     DECLARE @osql_for_nt int
  5281.     DECLARE @devnum int
  5282.     --DECLARE @num_pages int
  5283.     DECLARE @retcode int
  5284.     DECLARE @reg_key nvarchar(255)
  5285.     DECLARE @agentname nvarchar(100)
  5286.     DECLARE @command nvarchar (2048)
  5287.     DECLARE @distbit int
  5288.     DECLARE @install_path nvarchar(255)
  5289.     DECLARE @mssql_data_path nvarchar(255)
  5290.     DECLARE @on_clause nvarchar(512)
  5291.     DECLARE @logon_clause nvarchar(512)
  5292.     DECLARE @distproc nvarchar(255)
  5293.     DECLARE @major_version int
  5294.     DECLARE @db_exists bit
  5295.     DECLARE @trunc_log_bit int
  5296.     DECLARE @description nvarchar(100)
  5297.     DECLARE @category_name sysname
  5298.     DECLARE @createmode_attach int
  5299.     DECLARE @createmode_noattach int
  5300.     DECLARE @createmode_fordetach int
  5301.  
  5302.     DECLARE @filegrowth nvarchar(10)
  5303.     DECLARE @data_file_size_str nvarchar(10)
  5304.     DECLARE @log_file_size_str nvarchar(10)
  5305.     DECLARE @platform_nt binary
  5306.  
  5307.     --DECLARE @max_datafile_size int
  5308.     --DECLARE @max_logfile_size int
  5309.     
  5310.     IF @password = N''
  5311.         select @password = NULL
  5312.  
  5313.     select @platform_nt = 0x1
  5314.     select @filegrowth = N'512KB'
  5315.  
  5316.     -- on error, delete the data and log files only if they didn't pre-exist.
  5317.     -- by default, assume they pre-exist.
  5318.     select @data_file_preexists = 1
  5319.     select @log_file_preexists = 1
  5320.  
  5321.     if (@data_file_size IS NULL) or (@data_file_size = 0)
  5322.         select @data_file_size_str = N'512KB'
  5323.     else
  5324.         select @data_file_size_str = convert(nvarchar(10), @data_file_size)
  5325.  
  5326.     if (@log_file_size IS NULL) or (@log_file_size = 0)
  5327.         select @log_file_size_str = N'512KB'
  5328.     else
  5329.         select @log_file_size_str = convert(nvarchar(10), @log_file_size)
  5330.     
  5331.     --if (@data_file_size > 16)
  5332.     --  select @max_datafile_size = @data_file_size
  5333.     --else
  5334.     --  select @max_datafile_size = 16
  5335.     
  5336.     --if (@log_file_size > 16)
  5337.     --  select @max_logfile_size = @log_file_size
  5338.     --else
  5339.     --  select @max_logfile_size = 16
  5340.  
  5341.     select @createmode_attach = 0, @createmode_noattach = 1, @createmode_fordetach = 2
  5342.     SELECT @trunc_log_bit = 8
  5343.     SELECT @distbit = 16
  5344.  
  5345.     if (@createmode <> @createmode_fordetach)
  5346.     begin
  5347.     
  5348.         /* 
  5349.         ** Check if replication components are installed on this server
  5350.         */
  5351.         exec @retcode = dbo.sp_MS_replication_installed
  5352.         if (@retcode <> 1)
  5353.         begin
  5354.             return (1)
  5355.         end
  5356.     
  5357.         /* 
  5358.         ** Check for invalid security modes
  5359.         */
  5360.         IF @security_mode < 0 OR @security_mode > 1
  5361.         BEGIN
  5362.             RAISERROR(14109, 16, -1)
  5363.             RETURN (1)
  5364.         END
  5365.  
  5366.         IF ( ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  5367.         BEGIN
  5368.             RAISERROR(21038, 16, -1)
  5369.             RETURN (1)
  5370.         END
  5371.     
  5372.         /* 
  5373.         ** Check for invalid retention values 
  5374.         */
  5375.         IF @min_distretention < 0 OR @max_distretention < 0 
  5376.         BEGIN
  5377.             RAISERROR(14106, 16, -1)
  5378.             RETURN (1)
  5379.         END
  5380.         IF @min_distretention > @max_distretention
  5381.         BEGIN
  5382.             RAISERROR(14107, 16, -1) 
  5383.             RETURN (1)
  5384.         END
  5385.  
  5386.         /*
  5387.         ** Check to make sure this is a distributor
  5388.         */
  5389.         IF NOT EXISTS (SELECT * FROM master..sysservers
  5390.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME)
  5391.                  AND srvstatus & 8 <> 0)
  5392.         BEGIN
  5393.             RAISERROR (14114, 16, -1, @@SERVERNAME)
  5394.             RETURN(1)
  5395.         END
  5396.     
  5397.         /*
  5398.         ** Check if database is already configured as a distributor database
  5399.         */
  5400.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database)
  5401.         BEGIN
  5402.             RAISERROR (14119, 16, -1, @database)
  5403.             RETURN(1)
  5404.         END    
  5405.     end
  5406.  
  5407.     /* 
  5408.     ** Get installation path
  5409.     */
  5410.     EXECUTE @retcode = master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
  5411.               'SOFTWARE\Microsoft\MSSQLServer\Setup',
  5412.               'SQLPath',
  5413.             @param = @install_path OUTPUT
  5414.     IF @retcode <> 0 or @install_path is NULL or @install_path=''
  5415.     BEGIN
  5416.         GOTO UNDO       
  5417.     END
  5418.  
  5419.     /*
  5420.     ** Get the MSSQL DATA path. Note that users can have a SQLDataRoot directory different from SQLPath
  5421.     */
  5422.     EXECUTE @retcode = master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
  5423.               'SOFTWARE\Microsoft\MSSQLServer\Setup',
  5424.               'SQLDataRoot',
  5425.             @param = @mssql_data_path OUTPUT
  5426.     IF @retcode <> 0 or @mssql_data_path is NULL or @mssql_data_path = ''
  5427.     BEGIN
  5428.         GOTO UNDO       
  5429.     END
  5430.  
  5431.     IF @data_folder IS NULL or @data_folder = ''
  5432.         select @data_folder = @mssql_data_path + '\DATA'
  5433.  
  5434.     IF @log_folder IS NULL or @log_folder = ''
  5435.         select @log_folder = @mssql_data_path + '\DATA'
  5436.  
  5437.     IF @data_file IS NULL
  5438.         SELECT @data_file = @database + '.MDF'
  5439.  
  5440.     IF @log_file IS NULL
  5441.         SELECT @log_file = @database + '.LDF'
  5442.  
  5443.     if substring(@data_folder, len(@data_folder), 1) = '\'
  5444.         select @data_folder = substring (@data_folder, 1, len(@data_folder) -1)
  5445.     if substring(@log_folder, len(@log_folder), 1) = '\'
  5446.         select @log_folder = substring (@log_folder, 1, len(@log_folder) -1)
  5447.  
  5448.     SELECT @data_path = @data_folder + '\' + @data_file
  5449.     SELECT @log_path = @log_folder + '\' + @log_file
  5450.  
  5451.     SELECT @data_path_quoted_for_copy = '"' + @data_folder + '"' + '\' + '"' + @data_file + '"'
  5452.     SELECT @log_path_quoted_for_copy = '"' + @log_folder + '"' + '\' + '"' + @log_file + '"'
  5453.  
  5454.     select @logical_data_file = @database
  5455.  
  5456.     /* 
  5457.     ** Bug 40096: Truncate the logical log file name back to 128 characters
  5458.     **            long so the 'CREATE DATABASE' statement won't complain.
  5459.     */
  5460.     /* LEN(@logical_log_file) = LEN(@database) + LEN('_log') and
  5461.        LEN(@logical_log_file) <= 128 implies 
  5462.        LEN(@database) <=124 */
  5463.     IF (LEN(@database) > 124)
  5464.         SELECT @logical_log_file = SUBSTRING(@database, 1, 124) + '_log'  
  5465.     ELSE 
  5466.         SELECT @logical_log_file = @database + '_log'
  5467.  
  5468.     if (@createmode = @createmode_attach)
  5469.     begin
  5470.         select @canneddbdata_file = @mssql_data_path + '\DATA\DISTMDL.MDF'
  5471.         select @canneddblog_file = @mssql_data_path + '\DATA\DISTMDL.LDF'
  5472.  
  5473.         exec @file_exists = dbo.sp_MSexists_file @canneddbdata_file, 'DISTMDL.MDF'
  5474.         if (@file_exists = 0)
  5475.         begin
  5476.             /* Fallback to mode where instdist.sql needs to be run */
  5477.             select @createmode = @createmode_noattach
  5478.         end
  5479.  
  5480.         exec @file_exists = dbo.sp_MSexists_file @canneddblog_file, 'DISTMDL.LDF'
  5481.         if (@file_exists = 0)
  5482.         begin
  5483.             /* Fallback to mode where instdist.sql needs to be run */
  5484.             select @createmode = @createmode_noattach
  5485.         end
  5486.     end
  5487.  
  5488.     /*
  5489.     ** Create the distributor database if it does not exist
  5490.     */
  5491.     IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database) AND (@createmode <> @createmode_attach)
  5492.     BEGIN
  5493.  
  5494.         IF @logical_data_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_data_file)
  5495.         BEGIN
  5496.             SELECT @on_clause = ' ON (NAME =''' + @logical_data_file + ''',FILENAME=''' + @data_path + 
  5497.                 ''', SIZE=' + @data_file_size_str + ', FILEGROWTH=' + @filegrowth + ', MAXSIZE = UNLIMITED)'
  5498.         END
  5499.  
  5500.         IF @logical_log_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_log_file)
  5501.         BEGIN
  5502.             SELECT @logon_clause = ' LOG ON (NAME =''' + @logical_log_file + ''',FILENAME=''' + @log_path + 
  5503.                 ''', SIZE=' + @log_file_size_str + ', FILEGROWTH=' + @filegrowth + ', MAXSIZE= UNLIMITED)'          
  5504.         END
  5505.  
  5506.         /*
  5507.         ** Create distributor database
  5508.         */
  5509.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  5510.             + isnull(@on_clause, ' ') + isnull(@logon_clause, ' ')
  5511.  
  5512.         EXEC (@command)
  5513.         IF @@ERROR <> 0
  5514.             RETURN (1)
  5515.         SELECT @db_exists = 0
  5516.     END
  5517.     ELSE IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database) AND (@createmode = @createmode_attach)
  5518.     BEGIN
  5519.     /* DO THE CREATE DATABASE FOR ATTACH STUFF */
  5520.         
  5521.         exec @data_file_preexists = dbo.sp_MSexists_file @data_path_quoted_for_copy, @data_file
  5522.         if (@data_file_preexists = 1)
  5523.         begin
  5524.             raiserror(5170, 16, -1, @data_path)
  5525.             return 1
  5526.         end
  5527.     
  5528.         SELECT @on_clause = ' ON (NAME = ''' + @logical_data_file + ''', FILENAME=''' + @data_path + ''')'
  5529.         
  5530.         exec @log_file_preexists = dbo.sp_MSexists_file @log_path_quoted_for_copy, @log_file
  5531.         if (@log_file_preexists = 1)
  5532.         begin
  5533.             raiserror(5170, 16, -1, @log_path)
  5534.             return 1
  5535.         end
  5536.  
  5537.         SELECT @logon_clause = ' LOG ON (NAME = ''' + @logical_log_file + ''', FILENAME=''' + @log_path + ''')'
  5538.  
  5539.         select @filecopy_cmd = 'copy ' + @canneddbdata_file + ' ' + @data_path_quoted_for_copy
  5540.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  5541.         IF @retcode <> 0 OR @@ERROR <> 0
  5542.         BEGIN
  5543.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  5544.             return (1)
  5545.         END
  5546.  
  5547.         select @filecopy_cmd = 'copy ' + @canneddblog_file + ' ' + @log_path_quoted_for_copy
  5548.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  5549.         IF @retcode <> 0 OR @@ERROR <> 0
  5550.         BEGIN
  5551.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  5552.             return (1)
  5553.         END
  5554.  
  5555.         /*
  5556.         ** Create distributor database
  5557.         */
  5558.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  5559.             + @on_clause + @logon_clause + ' FOR ATTACH'
  5560.         dbcc traceon (1806)
  5561.         EXEC (@command)
  5562.         IF @@ERROR <> 0
  5563.         begin
  5564.             dbcc traceoff(1806)
  5565.             RETURN (1)
  5566.         end
  5567.         dbcc dbreindexall(@database, 240)
  5568.         dbcc traceoff(1806)
  5569.  
  5570.         SELECT @db_exists = 0
  5571.     END
  5572.     ELSE
  5573.     BEGIN
  5574.         SELECT @db_exists = 1
  5575.     END
  5576.  
  5577.     -- Must make the dist db owned by sa so that the sps in it can select from
  5578.     -- security cache tables in tempdb by owership chain rule.
  5579.     declare @retcode2 int
  5580.     select @retcode2 = 0
  5581.     select @distproc = QUOTENAME(@database) + '.dbo.sp_executesql'
  5582.     SELECT @command = 
  5583.         -- If the db is created by sa or from attach, sa is dbo already.
  5584.         -- sp_changedbowner will fail is the new owner is an user in the db already.
  5585.         ' if not exists (select * from sysusers where sid = 0x01) ' +
  5586.         ' exec @retcode2 = dbo.sp_changedbowner ''sa'''
  5587.     EXEC @retcode = @distproc @command, N'@retcode2 int output', @retcode2 output
  5588.     IF @retcode <> 0 or @retcode2 <> 0 or @@ERROR <> 0
  5589.     BEGIN
  5590.         GOTO UNDO
  5591.     END
  5592.  
  5593.     /* Set the database option truncate log on checkpoint & turn off autoclose which is default of win9x*/
  5594.     IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE 
  5595.         name = @database AND
  5596.         (status & @trunc_log_bit) = 0 )   -- if its not already marked
  5597.     BEGIN
  5598.         EXEC @retcode = dbo.sp_dboption @database, 'trunc. log on chkpt.', 'true'
  5599.         IF @retcode <> 0 OR @@ERROR <> 0
  5600.         BEGIN
  5601.             GOTO UNDO
  5602.         END
  5603.     END
  5604.  
  5605.     EXEC @retcode = dbo.sp_dboption @database, 'autoclose', 'false'
  5606.         IF @retcode <> 0 OR @@ERROR <> 0
  5607.         BEGIN
  5608.             GOTO UNDO
  5609.         END
  5610.  
  5611.     /*
  5612.     **
  5613.     ** Update sysdatabase category bit
  5614.     ** This is to prevent user from dropping the database.
  5615.     **/
  5616.     if (@createmode <> @createmode_fordetach)
  5617.     begin
  5618.         UPDATE master..sysdatabases SET category = category | @distbit WHERE name = @database
  5619.         IF @@ERROR <> 0
  5620.         BEGIN
  5621.             GOTO UNDO
  5622.         END
  5623.     end
  5624.  
  5625.     /* 
  5626.     ** Install instdist.sql
  5627.     */
  5628.  
  5629.     if (@createmode <> @createmode_attach) OR (@db_exists = 1)
  5630.     begin
  5631.     if (( platform() & @platform_nt = @platform_nt ))
  5632.         select @osql_for_nt = 1
  5633.     else
  5634.         select @osql_for_nt = 0
  5635.  
  5636.         -- Always use integrated security on WINNT since @login passed-in is for remote 
  5637.         -- subscriber and may not have enough privilege to apply the script
  5638.         IF (@security_mode = 1 or @osql_for_nt = 1) AND NOT (@security_mode = 0 AND @createmode = 2)
  5639.         BEGIN
  5640.             SELECT @osql_cmd = '" "' + @install_path + '\binn\osql" -E ' 
  5641.         END
  5642.         ELSE
  5643.         BEGIN
  5644.         -- cannot specify -S w/ -E for local execution, SID does not map due to nofix bug
  5645.         if (@osql_for_nt = 1)
  5646.             SELECT @osql_cmd = '" "' + @install_path + '\binn\osql" -U' + @login + ' -P' + 
  5647.                 isnull(@password,'') + ' -S' + @@SERVERNAME + ' '
  5648.         else
  5649.         SELECT @osql_cmd = '"' + @install_path + '\binn\osql" -U' + @login + ' -P' + 
  5650.                 isnull(@password,'') + ' -S' + @@SERVERNAME + ' '
  5651.         END
  5652.     
  5653.         select @osql_cmd = @osql_cmd + '-l60 -t60 '
  5654.  
  5655.         -- We must use -b option to make osql return error code !!
  5656.         SELECT @osql_cmd = @osql_cmd + 
  5657.             ' -d' + @database + ' -b ' +
  5658.             ' -i' + '"' + @install_path + '\install\instdist.sql"' + 
  5659.             ' -o' + '"' + @install_path + '\install\instdist.out"'
  5660.  
  5661.         if (@osql_for_nt = 1)
  5662.         BEGIN
  5663.             SELECT @osql_cmd = @osql_cmd + ' "'
  5664.         END
  5665.     
  5666.         EXEC @retcode = master..xp_cmdshell @osql_cmd
  5667.         IF @retcode <> 0 OR @@ERROR <> 0
  5668.         BEGIN
  5669.             RAISERROR (14113, 16, -1, @osql_cmd, 'instdist.out')
  5670.             GOTO UNDO       
  5671.         END
  5672.     end
  5673.     
  5674.     if (@createmode <> @createmode_fordetach)
  5675.     begin
  5676.         /* Set db_existed bit in MSrepl_version */
  5677.         IF @db_exists = 1
  5678.         BEGIN
  5679.             SELECT @distproc = 'UPDATE ' +
  5680.                 @database + '..MSrepl_version SET db_existed = 0x1'
  5681.     
  5682.             EXEC(@distproc)
  5683.             IF @@ERROR <> 0
  5684.             BEGIN
  5685.                 GOTO UNDO       
  5686.             END
  5687.         END
  5688.  
  5689.         DELETE msdb.dbo.MSdistributiondbs WHERE name = @database
  5690.         IF @@ERROR <> 0
  5691.         BEGIN
  5692.             GOTO UNDO       
  5693.         END
  5694.  
  5695.         INSERT INTO msdb.dbo.MSdistributiondbs VALUES (
  5696.             @database, @min_distretention, @max_distretention, @history_retention
  5697.             )
  5698.         IF @@ERROR <> 0
  5699.         BEGIN
  5700.             GOTO UNDO       
  5701.         END
  5702.  
  5703.         -- This login need db_owner priviledge to call sps in distribution db
  5704.         declare @distributor_login sysname
  5705.         select @distributor_login = 'distributor_admin'
  5706.  
  5707.         select @command = quotename(@database) + '.dbo.sp_MSrepl_dbrole'
  5708.         exec @retcode = @command 'db_owner', @distributor_login, 'add'
  5709.         IF @@error <> 0 OR @retcode <> 0
  5710.             GOTO UNDO
  5711.  
  5712.         /*
  5713.         ** Create the history cleanup agent.
  5714.         */
  5715.         SELECT @agentname = formatmessage (20567, @database)
  5716.         SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  5717.             CONVERT(nvarchar(12), @history_retention)
  5718.  
  5719.         IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname and
  5720.             originating_server = '(local)')
  5721.         BEGIN
  5722.             EXEC @retcode = msdb.dbo.sp_delete_job 
  5723.                 @job_name = @agentname
  5724.             IF @@ERROR <> 0 or @retcode <> 0
  5725.             BEGIN
  5726.                 GOTO UNDO
  5727.             END
  5728.         END
  5729.  
  5730.         set @description = formatmessage(20535)
  5731.  
  5732.         -- Get History Cleanup category name (assumes category_id = 12)
  5733.         select @category_name = name FROM msdb.dbo.syscategories where category_id = 12
  5734.  
  5735.         EXECUTE @retcode = dbo.sp_MSadd_repl_job @agentname,
  5736.         @subsystem = 'TSQL',
  5737.         @server = @@SERVERNAME,
  5738.         @databasename = @database,
  5739.         @description = @description,
  5740.         @freqtype = 4,    
  5741.         @freqsubtype = 4,         
  5742.         @freqsubinterval = 10,    /* Number of minutes between runs */ 
  5743.         @command = @command,
  5744.         @enabled = 1,
  5745.         @retryattempts = 0,
  5746.         @loghistcompletionlevel = 0,
  5747.         @category_name = @category_name
  5748.     
  5749.         IF @@ERROR <> 0 or @retcode <> 0
  5750.         BEGIN
  5751.             GOTO UNDO
  5752.         END
  5753.  
  5754.         /*
  5755.         ** Create the distribution cleanup agent.
  5756.         */
  5757.         SELECT @agentname = formatmessage (20568, @database)
  5758.         SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  5759.             CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  5760.             CONVERT(nvarchar(12), @max_distretention)
  5761.  
  5762.         IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname and
  5763.             originating_server = '(local)')
  5764.         BEGIN
  5765.             EXEC @retcode = msdb.dbo.sp_delete_job 
  5766.                 @job_name = @agentname
  5767.             IF @@ERROR <> 0 or @retcode <> 0
  5768.             BEGIN
  5769.                 GOTO UNDO
  5770.             END
  5771.         END
  5772.  
  5773.         set @description = formatmessage(20541)
  5774.         -- Get Distribution Cleanup category name (assumes category_id = 11)
  5775.         select @category_name = name FROM msdb.dbo.syscategories where category_id = 11
  5776.     
  5777.         EXECUTE @retcode = msdb.dbo.sp_MSadd_repl_job @agentname,
  5778.         @subsystem = 'TSQL',
  5779.         @server = @@SERVERNAME,
  5780.         @databasename = @database,
  5781.         @description = @description,
  5782.         @freqtype = 4,    
  5783.         @freqsubtype = 4,         
  5784.         @freqsubinterval = 10,    /* Number of minutes between runs */ 
  5785.         @command = @command,
  5786.         @retryattempts = 0,
  5787.         @enabled = 0,
  5788.         @loghistcompletionlevel = 0,
  5789.         @category_name = @category_name
  5790.         IF @@ERROR <> 0 or @retcode <> 0
  5791.         BEGIN
  5792.             GOTO UNDO
  5793.         end
  5794.     end
  5795.     else
  5796.     begin
  5797.         /*detach */
  5798.         dbcc detachdb(@database)
  5799.     end
  5800.     
  5801.     RETURN(0)
  5802.  
  5803. UNDO:
  5804.  
  5805.     IF @db_exists = 0
  5806.         EXECUTE dbo.sp_dropdistributiondb @database
  5807.  
  5808.     /* Need to do it since sp_dropdistributiondb will fail in some cases */
  5809.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database
  5810.     
  5811.     DELETE msdb.dbo.MSdistributiondbs where name = @database
  5812.  
  5813.     /* drop the database and ignore error */
  5814.     IF @db_exists = 0 AND
  5815.         EXISTS (SELECT * from master..sysdatabases WHERE name = @database)
  5816.     BEGIN
  5817.         SELECT @command = 'USE master  DROP DATABASE ' +  QUOTENAME(@database) 
  5818.         EXEC (@command)
  5819.     END
  5820.  
  5821.     if (@createmode = @createmode_attach)
  5822.     begin
  5823.         if (@data_file_preexists = 0)
  5824.         begin
  5825.             select @command = 'del ' + @data_path_quoted_for_copy
  5826.             exec master..xp_cmdshell @command
  5827.             --ignore errors
  5828.         end
  5829.         if (@log_file_preexists = 0)
  5830.         begin
  5831.             select @command = 'del ' + @log_path_quoted_for_copy
  5832.             exec master..xp_cmdshell @command
  5833.             --ignore errors
  5834.         end
  5835.     end
  5836.         
  5837.     RETURN(1)        
  5838. GO
  5839.  
  5840. raiserror('Creating procedure sp_MSexists_file', 0,1)
  5841. go
  5842. create procedure sp_MSexists_file (@full_path nvarchar(512), @filename nvarchar(255))
  5843. AS
  5844.     SET NOCOUNT ON
  5845.  
  5846.     DECLARE @command nvarchar(512)
  5847.     DECLARE @retcode int
  5848.     DECLARE @platform_nt int
  5849.  
  5850.     select @platform_nt = 1
  5851.  
  5852.     if (@full_path is NULL)
  5853.     begin
  5854.         raiserror(14043,16,-1, '@full_path')
  5855.         return -1
  5856.     end
  5857.  
  5858.     if (@filename is NULL)
  5859.     begin
  5860.         raiserror(14043,16,-1, '@filename')
  5861.         return -1
  5862.     end
  5863.  
  5864.     select @command = 'dir ' + @full_path
  5865.  
  5866.     if (platform() & @platform_nt = @platform_nt)
  5867.     begin   
  5868.         exec @retcode = master..xp_cmdshell @command, 'no_output'
  5869.         if @@error <> 0
  5870.             return -1
  5871.         if @retcode <> 0 
  5872.             select @retcode = 0
  5873.         else
  5874.             select @retcode = 1
  5875.     end
  5876.     else
  5877.     begin
  5878.         create table #DirectoryTempTable(cmdoutput nvarchar(255) null)
  5879.         insert into #DirectoryTempTable exec master..xp_cmdshell @command
  5880.  
  5881.         /*
  5882.         ** The return code from xp_cmdshell is not a reliable way to check whether the file exists or
  5883.         ** not. It is always 0 on Win95 as long as xp_cmdshell succeeds.
  5884.         */  
  5885.     
  5886.         if exists (select * from #DirectoryTempTable where ltrim(rtrim(cmdoutput)) like '%'+@filename+'%') 
  5887.             and not exists (select * from #DirectoryTempTable where ltrim(rtrim(cmdoutput)) like '%File Not Found%')
  5888.             select @retcode = 1
  5889.         else 
  5890.             select @retcode = 0
  5891.  
  5892.         drop table #DirectoryTempTable
  5893.     end
  5894.  
  5895.     return @retcode
  5896. go
  5897.  
  5898.  
  5899. dump tran master with no_log
  5900. GO
  5901.  
  5902. raiserror('Creating procedure sp_dropdistpublisher', 0,1)
  5903. go
  5904.  
  5905. CREATE PROCEDURE sp_dropdistpublisher (
  5906.     @publisher sysname,         /* publisher server name */
  5907.     @no_checks bit = 0
  5908.         ) AS
  5909.  
  5910.     SET NOCOUNT ON
  5911.  
  5912.     DECLARE @distributor sysname
  5913.     DECLARE @distaccount nvarchar(127)
  5914.     DECLARE @proc nvarchar (255)
  5915.     DECLARE @retcode int
  5916.     DECLARE @privilege sysname
  5917.     DECLARE @reg_key nvarchar(255)
  5918.     DECLARE @return_status int
  5919.     DECLARE @foundSubscriber int 
  5920.     DECLARE @distribdb sysname
  5921.     DECLARE @command nvarchar(255)
  5922.     DECLARE @active_value int
  5923.     DECLARE @publish_bit int
  5924.     DECLARE @mergepub_bit int
  5925.  
  5926.  
  5927.     SELECT @return_status = 0
  5928.     SELECT @foundSubscriber = 0
  5929.  
  5930.     SELECT @publish_bit = 1
  5931.     SELECT @mergepub_bit = 4
  5932.  
  5933.     /*
  5934.     ** Parameter Check:  @publisher.
  5935.     ** Check to make sure that the publisher exists, that the name isn't
  5936.     ** NULL, and that the name conforms to the rules for identifiers.
  5937.     */
  5938.  
  5939.     IF @publisher IS NULL
  5940.         BEGIN
  5941.             RAISERROR (14043, 16, -1, '@publisher')
  5942.             RETURN (1)
  5943.         END
  5944.  
  5945.     EXECUTE @retcode = dbo.sp_validname @publisher
  5946.  
  5947.     IF @retcode <> 0
  5948.         RETURN (1)
  5949.  
  5950.     /*
  5951.     ** Get distribution server information for remote RPC
  5952.     ** agent verification.
  5953.     */
  5954.     EXEC @retcode = dbo.sp_helpdistributor @publisher = @publisher,
  5955.         @distributor = @distributor OUTPUT,
  5956.         @distribdb = @distribdb OUTPUT
  5957.     IF @@error <> 0 OR @retcode <> 0
  5958.         BEGIN
  5959.         RAISERROR (14071, 16, -1)
  5960.            RETURN (1)
  5961.     END
  5962.  
  5963.     /*
  5964.     ** Only a local distributor can be modified.
  5965.     */
  5966.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  5967.         BEGIN
  5968.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5969.         RETURN (1)
  5970.         END
  5971.  
  5972.     IF NOT EXISTS (SELECT *
  5973.     FROM msdb..MSdistpublishers
  5974.             WHERE UPPER(name) = UPPER(@publisher))
  5975.     BEGIN
  5976.         RAISERROR (14080, 11, -1)
  5977.         RETURN (1)
  5978.     END
  5979.  
  5980.  
  5981.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  5982.     BEGIN
  5983.         /* 
  5984.         ** If @publisher is local, 
  5985.         ** 1. check to make sure there is no subscriber 
  5986.         ** for the publisher in the distribution database
  5987.         ** 2. check to make sure there are not database enabled for replication
  5988.         */
  5989.         -- Check to see if there are subscribers defined.
  5990.         if exists (SELECT *
  5991.                      FROM master..sysservers
  5992.                     WHERE srvstatus & 4 <> 0)
  5993.         begin
  5994.             raiserror(21047, 16, -1)
  5995.             return(1)
  5996.         end
  5997.         
  5998.         IF EXISTS (SELECT * FROM master..sysdatabases WHERE 
  5999.             (category & @publish_bit) <> 0 OR
  6000.             (category & @mergepub_bit) <> 0)
  6001.         BEGIN 
  6002.             RAISERROR (21033, 16, -1, @@SERVERNAME)
  6003.             RETURN (1)
  6004.         END
  6005.     END
  6006.     ELSE
  6007.     BEGIN
  6008.         if @no_checks = 0
  6009.         begin
  6010.             /* 
  6011.             ** If the publisher is remote, check the status of the distpublisher 
  6012.             ** The status will be inactive if the remote publisher dropped the
  6013.             ** distributor.
  6014.             */
  6015.  
  6016.             IF EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  6017.                 UPPER(name) = UPPER(@publisher) and active = 1)
  6018.             BEGIN
  6019.                 RAISERROR (14098, 16, -1, @publisher, @@SERVERNAME)
  6020.                 RETURN (1)
  6021.             END
  6022.         end
  6023.         else
  6024.         begin
  6025.             SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  6026.             exec @retcode = @command @publisher
  6027.             if @retcode <> 0 or @@error <> 0
  6028.                 return 1
  6029.         end
  6030.     END
  6031.  
  6032.     DELETE msdb..MSdistpublishers where UPPER(name) = UPPER(@publisher)
  6033.     IF @@ERROR <> 0  RETURN (1)
  6034.  
  6035.  
  6036.     declare @fExists int
  6037.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6038.     if (@fExists = 1)
  6039.     BEGIN
  6040.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin', sa
  6041.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6042.     END
  6043.  
  6044.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 
  6045.         'distributor_admin'
  6046.     if (@fExists = 1)
  6047.     BEGIN
  6048.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin',
  6049.             'distributor_admin'
  6050.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6051.     END
  6052.  
  6053. /* SECURITY ********************************
  6054.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6055.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6056.        WHERE UPPER(srvname) = UPPER(@publisher))
  6057.        AND remoteusername = 'sa'
  6058.        AND suid = 1)     --'sa' 
  6059. *********************************/
  6060.  
  6061.  
  6062. /* SECURITY ********************************
  6063.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6064.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6065.        WHERE UPPER(srvname) = UPPER(@publisher))
  6066.        AND remoteusername = 'probe'
  6067.        AND suid = 10)     -- 'probe' 
  6068.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  6069.     if (@fExists = 1)
  6070.     BEGIN
  6071.        EXECUTE @retcode = dbo.sp_remoteoption @publisher, probe, probe, trusted, false
  6072.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6073.     END
  6074. *********************************/
  6075.  
  6076.     return(@return_status)
  6077. GO
  6078.  
  6079. raiserror('Creating procedure sp_adddistpublisher', 0,1)
  6080. go
  6081.  
  6082. CREATE PROCEDURE sp_adddistpublisher (
  6083.     @publisher sysname,      /* publisher server name */
  6084.     @distribution_db sysname,
  6085.     @security_mode int = NULL,
  6086.     @login sysname = 'sa',
  6087.     @password sysname = NULL,
  6088.     @working_directory nvarchar(255),
  6089.     @trusted nvarchar(5) = NULL,
  6090.     @encrypted_password bit = 0,
  6091.     @thirdparty_flag bit = 0
  6092.         ) AS
  6093.  
  6094.     SET NOCOUNT ON
  6095.  
  6096.     /*
  6097.     ** Declarations.
  6098.     */
  6099.  
  6100.     DECLARE @retcode int
  6101.     DECLARE @reg_key nvarchar(255)
  6102.     DECLARE @distbit int
  6103.     DECLARE @active_value int
  6104.     DECLARE @server_added bit
  6105.     DECLARE @proc nvarchar(255)
  6106.     declare @fExists int
  6107.     declare @command nvarchar(255)
  6108.     declare @trusted_id bit
  6109.     declare @platform_nt binary
  6110.     declare @qv_replication varchar(10)
  6111.     declare @qv_replication_unlimited integer
  6112.     declare @qv_value_replication integer
  6113.  
  6114.     select @platform_nt = 0x1
  6115.     select @qv_replication = '2745196162', @qv_replication_unlimited = 0
  6116.  
  6117.     SELECT @distbit = 16
  6118.     SELECT @server_added = 0
  6119.  
  6120.     /* 
  6121.     ** Check if replication components are installed on this server
  6122.     */
  6123.     exec @retcode = dbo.sp_MS_replication_installed
  6124.     if (@retcode <> 1)
  6125.     begin
  6126.         return (1)
  6127.     end
  6128.  
  6129.     IF @working_directory IS NULL or ltrim(rtrim(@working_directory)) = ' '
  6130.     BEGIN
  6131.         RAISERROR (14043, 16, -1, '@working_directory')
  6132.         GOTO UNDO
  6133.     END
  6134.  
  6135.  
  6136.     /*
  6137.     ** Parameter Check:  @publisher.
  6138.     ** Check to make sure that the publisher is not NULL and that it
  6139.     ** conforms to the rules for identifiers.
  6140.     */
  6141.  
  6142.     IF @publisher IS NULL
  6143.         BEGIN
  6144.             RAISERROR (14043, 16, -1, '@publisher')
  6145.             GOTO UNDO
  6146.         END
  6147.  
  6148.     EXECUTE @retcode = dbo.sp_validname @publisher
  6149.  
  6150.     IF @@ERROR <> 0 OR @retcode <> 0
  6151.         GOTO UNDO
  6152.  
  6153.     IF @password = N''
  6154.         select @password = NULL
  6155.  
  6156.     /* On REPLICATION_LIMITED server, only local publisher is supported.
  6157.      * Note: The login and password registered for local publisher will be used for 
  6158.      * local agents to login to distributor, thus local publisher has to be installed first.
  6159.      * We choose not to support remote dist publshers on REPLICATION_LIMITED server altogether.
  6160.      * On NT, local agents will always use integrated security to log into 
  6161.      * distributor
  6162.      * Today, REPLICATION_LIMITED means desktop but we check specific sku entry just in case
  6163.     */
  6164.     exec @qv_value_replication = master.dbo.xp_qv @qv_replication     
  6165.     if ( @qv_value_replication != @qv_replication_unlimited ) and ( UPPER(@publisher) <> UPPER(@@servername) )
  6166.     begin
  6167.         -- remote dist publisher is not supported on this server version
  6168.         raiserror(21041,16,-1)
  6169.         goto UNDO
  6170.     end
  6171.  
  6172.  
  6173.     -- Set default security
  6174.     IF @security_mode IS NULL
  6175.     BEGIN
  6176.         IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( platform() & @platform_nt = @platform_nt ) )
  6177.             SELECT @security_mode = 1
  6178.         ELSE
  6179.             SELECT @security_mode = 0
  6180.     END
  6181.  
  6182.     /* 
  6183.     ** Check for invalid security mode
  6184.     */
  6185.     IF @security_mode < 0 OR @security_mode > 1
  6186.         BEGIN
  6187.             RAISERROR(14109, 16, -1)
  6188.             GOTO UNDO
  6189.         END
  6190.  
  6191.     IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  6192.     BEGIN
  6193.         RAISERROR(21038, 16, -1)
  6194.         RETURN (1)
  6195.     END
  6196.  
  6197.     -- Encrypt the password
  6198.     IF @encrypted_password = 0
  6199.     BEGIN
  6200.         EXEC @retcode = master.dbo.xp_repl_encrypt @password OUTPUT
  6201.         IF @@error <> 0 OR @retcode <> 0
  6202.             return 1
  6203.     END
  6204.  
  6205.     -- Validate the working directory 
  6206.     -- Remove heading and trailing spaces
  6207.     select @working_directory = RTRIM(LTRIM(@working_directory))
  6208.     
  6209.     -- if the last char is '\', remove it.
  6210.     if substring(@working_directory, len(@working_directory),1) = '\'
  6211.         select @working_directory = substring(@working_directory, 1,
  6212.             len(@working_directory)-1)
  6213.  
  6214.     -- Don't do validation if it is a UNC path due to security problem.
  6215.     -- If the server is started as a service using local system account, we
  6216.     -- don't have access to the UNC path.
  6217.     if substring(@working_directory, 1,2) <> '\\'
  6218.     begin
  6219.         select @command = 'dir ' + @working_directory
  6220.         exec @retcode = master..xp_cmdshell @command, 'no_output'
  6221.         if @@error <> 0
  6222.             goto UNDO
  6223.         if @retcode <> 0 
  6224.         begin
  6225.             raiserror (21037, 16, -1, @working_directory)
  6226.             goto UNDO
  6227.         end
  6228.     end
  6229.  
  6230.     /*
  6231.     ** Parameter Check:  @trusted
  6232.     */
  6233.  
  6234.     if @trusted is null
  6235.     begin
  6236.         if UPPER(@publisher) = UPPER(@@servername)
  6237.             select @trusted = 'false'
  6238.         else
  6239.             select @trusted = 'true'
  6240.     end
  6241.  
  6242.     IF LOWER(@trusted) NOT IN ('true', 'false')
  6243.         BEGIN
  6244.             RAISERROR (14148, 16, -1, '@trusted')
  6245.             RETURN (1)
  6246.         END
  6247.  
  6248.     IF LOWER(@trusted) = 'true' SELECT @trusted_id = 1
  6249.     ELSE SELECT @trusted_id = 0
  6250.  
  6251.     /*
  6252.     ** Check to make sure this is a distributor
  6253.     */
  6254.     IF NOT EXISTS (SELECT * FROM master..sysservers
  6255.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME)
  6256.                  AND srvstatus & 8 <> 0)
  6257.     BEGIN
  6258.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6259.         GOTO UNDO
  6260.     END
  6261.  
  6262.     /*
  6263.     ** Check if database is configured as a distributor database
  6264.     */
  6265.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @distribution_db)
  6266.     BEGIN
  6267.         RAISERROR (14117, 16, -1, @distribution_db)
  6268.         GOTO UNDO
  6269.     END
  6270.  
  6271.     /* Check if publisher is already defined. */
  6272.     IF EXISTS (SELECT *
  6273.          FROM msdb..MSdistpublishers
  6274.         WHERE UPPER(name) = UPPER(@publisher))
  6275.  
  6276.     BEGIN
  6277.         RAISERROR (14074, 16, -1, @publisher)
  6278.         RETURN (1)
  6279.     END
  6280.  
  6281.  
  6282.  
  6283.     IF NOT EXISTS (SELECT *
  6284.              FROM master..sysservers
  6285.             WHERE UPPER(srvname) = UPPER(@publisher))
  6286.  
  6287.     /* Add the server if it does not exist. */
  6288.     BEGIN
  6289.         EXECUTE @retcode = dbo.sp_addserver @publisher
  6290.         IF @@error <> 0 OR @retcode <> 0
  6291.         BEGIN
  6292.             RAISERROR (14075, 16, -1)
  6293.             GOTO UNDO
  6294.         END
  6295.         SELECT @server_added = 1
  6296.     END
  6297.  
  6298.      
  6299.     /*
  6300.     ** Set the Active  value.
  6301.     ** If the @publisher is local, set it to true.
  6302.     ** Otherwise, set it to false
  6303.     */
  6304.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  6305.         SELECT @active_value = 1
  6306.     ELSE
  6307.         SELECT @active_value = 0
  6308.  
  6309.  
  6310.     DELETE msdb.dbo.MSdistpublishers where UPPER(name) = UPPER(@publisher)
  6311.     IF @@ERROR <> 0
  6312.     BEGIN
  6313.         GOTO UNDO       
  6314.     END
  6315.  
  6316.     INSERT INTO msdb.dbo.MSdistpublishers VALUES (
  6317.         @publisher, @distribution_db, @working_directory,
  6318.         @security_mode, @login, @password, @active_value, @trusted_id, @thirdparty_flag)
  6319.     IF @@ERROR <> 0
  6320.     BEGIN
  6321.         GOTO UNDO       
  6322.     END
  6323.  
  6324.     -- Add distributor_admin to distribution_admin non trusted mapping
  6325.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 
  6326.         'distributor_admin'
  6327.     if( @fExists = 0 )
  6328.     BEGIN
  6329.  
  6330.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 
  6331.             'distributor_admin'
  6332.         IF @@error <> 0 OR @retcode <> 0
  6333.         BEGIN
  6334.             RAISERROR (14075, 16, -1)
  6335.             GOTO UNDO
  6336.         END
  6337.     END
  6338.  
  6339.     -- For 6x publisher, we still need the trusted sa to sa.
  6340.     -- For 6x publisher upgrading to 7.0, distributor_admin to distributor_admin need to be trusted.
  6341.     -- add remotelogin of SA if it doesn't already exist
  6342.     -- If there's a mapping for remote login sa already, we cannot map it to distributor_admin
  6343.     -- this is the case of server upgraded from 6.5.
  6344.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 'sa'
  6345.     if( @fExists = 0 )
  6346.     BEGIN
  6347.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 'sa'
  6348.         IF @@error <> 0 OR @retcode <> 0
  6349.         BEGIN
  6350.             RAISERROR (14075, 16, -1)
  6351.             GOTO UNDO
  6352.         END
  6353.     END
  6354.     if @trusted_id = 1
  6355.     begin
  6356.         exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6357.         if( @fExists = 1 )
  6358.         BEGIN
  6359.             EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  6360.             IF @@error <> 0 OR @retcode <> 0
  6361.             BEGIN
  6362.                 RAISERROR (14075, 16, -1)
  6363.                 GOTO UNDO
  6364.             END
  6365.         END
  6366.  
  6367.         EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'distributor_admin', trusted, true
  6368.         IF @@error <> 0 OR @retcode <> 0
  6369.         BEGIN
  6370.             RAISERROR (14075, 16, -1)
  6371.             GOTO UNDO
  6372.         END
  6373.     END
  6374.  
  6375.  
  6376.     /* Add remotelogin enabling the 'probe' of the publisher to
  6377.     ** RPC for distribution counter information.
  6378.     */
  6379. /* SECURITY ********************************
  6380.     IF NOT EXISTS (SELECT *
  6381.               FROM master..sysremotelogins srl,
  6382.                master..sysservers ss
  6383.              WHERE ss.srvname = @publisher
  6384.            AND srl.remoteserverid = ss.srvid
  6385.            AND srl.remoteusername = 'probe'
  6386.        AND srl.suid = 10)    -- 'probe' 
  6387.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  6388.     if (@fExists = 0)
  6389.     BEGIN
  6390.        EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'probe', 'probe'
  6391.        IF @@error <> 0 OR @retcode <> 0
  6392.        BEGIN
  6393.         RAISERROR (14075, 16, -1)
  6394.         GOTO UNDO
  6395.        END
  6396.     END
  6397. *********************************/
  6398.     
  6399.     RETURN(0)
  6400.  
  6401. UNDO:
  6402.     -- If the server is marked, drop it
  6403.     IF EXISTS (SELECT *
  6404.          FROM msdb..MSdistpublishers
  6405.         WHERE UPPER(name) = UPPER(@publisher))
  6406.         EXEC dbo.sp_dropdistpublisher @publisher
  6407.  
  6408.     IF @server_added = 1
  6409.         EXEC dbo.sp_dropserver @publisher
  6410.  
  6411.     RETURN(1)
  6412. GO
  6413.  
  6414.  
  6415. raiserror('Creating procedure sp_changedistpublisher', 0,1)
  6416. go
  6417.  
  6418. CREATE PROCEDURE sp_changedistpublisher (
  6419.     @publisher sysname,
  6420.     @property sysname     = NULL,     /* The property to change */
  6421.     @value nvarchar(255)     = NULL      /* The new property value */
  6422.     ) AS
  6423.  
  6424.     SET NOCOUNT ON
  6425.  
  6426.     /*
  6427.     ** Declarations.
  6428.     */
  6429.     DECLARE @retcode int
  6430.     DECLARE @new_database sysname
  6431.     DECLARE @new_security_mode int
  6432.     DECLARE @new_login sysname
  6433.     DECLARE @new_password sysname
  6434.     DECLARE @distbit int
  6435.     DECLARE @new_active int
  6436.     DECLARE @new_trusted bit
  6437.     DECLARE @command nvarchar(255)
  6438.     declare @distribdb sysname
  6439.     DECLARE @platform_nt binary
  6440.  
  6441.     SELECT @platform_nt = 0x1
  6442.     SELECT @distbit = 16
  6443.     
  6444.     /*
  6445.     ** Parameter Check:  @property.
  6446.     ** If the @property parameter is NULL, print the options.
  6447.     */
  6448.  
  6449.     IF @property IS NULL
  6450.         BEGIN
  6451.             CREATE TABLE #tab1 (properties sysname NOT NULL)
  6452.             INSERT INTO #tab1 VALUES ('distribution_db')
  6453.             INSERT INTO #tab1 VALUES ('working_directory')
  6454.             INSERT INTO #tab1 VALUES ('security_mode')
  6455.             INSERT INTO #tab1 VALUES ('login')
  6456.             INSERT INTO #tab1 VALUES ('password')
  6457.             INSERT INTO #tab1 VALUES ('active')
  6458.             INSERT INTO #tab1 VALUES ('trusted')
  6459.             SELECT * FROM #tab1
  6460.             RETURN (0)
  6461.         END
  6462.  
  6463.     /*
  6464.     ** Parameter Check:  @property.
  6465.     ** Check to make sure that @property is a valid property in
  6466.     ** sysarticles.
  6467.     */
  6468.     IF @property IS NULL OR LOWER(@property) NOT IN ('distribution_db',
  6469.          'working_directory',
  6470.          'security_mode',
  6471.          'login',
  6472.          'password',
  6473.          'active',
  6474.          'trusted')
  6475.         BEGIN
  6476.             RAISERROR (14115, 16, -1, 
  6477.             '''distribution_db'', ''working_directory'', ''security_mode'', ''login'', ''password'', ''active'', ''or trusted''')
  6478.             RETURN (1)
  6479.         END
  6480.  
  6481.     /*
  6482.     ** Check to make sure this is a distributor
  6483.     */
  6484.     IF NOT EXISTS (SELECT * FROM master..sysservers
  6485.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME)
  6486.                  AND srvstatus & 8 <> 0)
  6487.     BEGIN
  6488.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6489.         RETURN(1)
  6490.     END
  6491.  
  6492.     -- Get the distribution db name.
  6493.     select @distribdb = distribution_db from msdb..MSdistpublishers where
  6494.         UPPER(name) = UPPER(@publisher)
  6495.  
  6496.     /*
  6497.     ** Change the property.
  6498.     */
  6499.     IF LOWER(@property) = 'distribution_db'
  6500.         BEGIN
  6501.             IF @value IS NULL
  6502.                 BEGIN
  6503.                     RAISERROR (14043, 16, -1, '@value') 
  6504.                     RETURN (1)
  6505.                 END
  6506.  
  6507.             IF @value <> @distribdb and 
  6508.                 EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  6509.                 UPPER(name) = UPPER(@publisher) and active = 1)
  6510.             BEGIN
  6511.                 RAISERROR (21046, 16, -1)
  6512.                 RETURN (1)
  6513.             END
  6514.  
  6515.             /*
  6516.             ** Check if database is configured as a distributor database
  6517.             */
  6518.             IF NOT EXISTS (SELECT * FROM master..sysdatabases
  6519.               WHERE name = @value
  6520.                  AND category & @distbit <> 0)
  6521.                 BEGIN
  6522.                     RAISERROR (14117, 16, -1, @new_database)
  6523.                     RETURN(1)
  6524.                 END
  6525.  
  6526.             UPDATE msdb..MSdistpublishers SET distribution_db = @value
  6527.                 WHERE UPPER(name) = UPPER(@publisher)
  6528.             IF @@error <> 0 
  6529.                 BEGIN
  6530.                     RETURN (1)
  6531.                 END
  6532.         END
  6533.    
  6534.     IF LOWER(@property) = 'working_directory'
  6535.         BEGIN
  6536.             IF @value IS NULL
  6537.                 BEGIN
  6538.                     RAISERROR (14043, 16, -1, '@value') 
  6539.                     RETURN (1)
  6540.                 END
  6541.  
  6542.             
  6543.             -- Validate the working directory 
  6544.             -- Remove heading and trailing spaces
  6545.             select @value = RTRIM(LTRIM(@value))
  6546.  
  6547.             -- if the last char is '\', remove it.
  6548.  
  6549.             if substring(@value, len(@value),1) = '\'
  6550.                 select @value = substring(@value, 1,
  6551.                     len(@value)-1)
  6552.  
  6553.             -- Don't do validation if it is a UNC path due to security problem.
  6554.             -- If the server is started as a service using local system account, we
  6555.             -- don't have access to the UNC path.
  6556.             if substring(@value, 1,2) <> '\\'
  6557.             begin
  6558.                 select @command = 'dir ' + @value
  6559.                 exec @retcode = master..xp_cmdshell @command, 'no_output'
  6560.                 if @@error <> 0
  6561.                     return 1
  6562.                 if @retcode <> 0 
  6563.                 begin
  6564.                     raiserror (21037, 16, -1, @value)
  6565.                     return 1
  6566.                 end
  6567.             end
  6568.  
  6569.             UPDATE msdb..MSdistpublishers SET working_directory = @value
  6570.                 WHERE UPPER(name) = UPPER(@publisher)
  6571.             IF @@error <> 0 
  6572.                 BEGIN
  6573.                     RETURN (1)
  6574.                 END
  6575.         END
  6576.  
  6577.     IF LOWER(@property) = 'security_mode'
  6578.         BEGIN
  6579.             IF @value IS NULL
  6580.                 BEGIN
  6581.                     RAISERROR (14043, 16, -1, '@value') 
  6582.                     RETURN (1)
  6583.                 END
  6584.  
  6585.             /*
  6586.             ** Set the SecurityMode registry key value
  6587.             */
  6588.             SELECT @new_security_mode = CONVERT(int, @value)
  6589.  
  6590.             /* 
  6591.             ** Check for invalid values 
  6592.             */
  6593.             IF @new_security_mode < 0 OR @new_security_mode > 1
  6594.                 BEGIN
  6595.                     RAISERROR(14109, 16, -1)
  6596.                     RETURN (1)
  6597.                 END
  6598.  
  6599.             IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @new_security_mode = 1)
  6600.             BEGIN
  6601.                 RAISERROR(21038, 16, -1)
  6602.                 RETURN (1)
  6603.             END
  6604.  
  6605.             UPDATE msdb..MSdistpublishers SET security_mode = @new_security_mode
  6606.                 WHERE UPPER(name) = UPPER(@publisher)
  6607.             IF @@error <> 0 
  6608.                 BEGIN
  6609.                     RETURN (1)
  6610.                 END
  6611.         END
  6612.  
  6613.     IF LOWER(@property) = 'login'
  6614.         BEGIN
  6615.             IF @value IS NULL
  6616.                 BEGIN
  6617.                     RAISERROR (14043, 16, -1, '@value') 
  6618.                     RETURN (1)
  6619.                 END
  6620.  
  6621.             /*
  6622.             ** Set the Login registry key value
  6623.             */
  6624.             SELECT @new_login = CONVERT(sysname, @value)
  6625.  
  6626.             UPDATE msdb..MSdistpublishers SET login = @new_login
  6627.                 WHERE UPPER(name) = UPPER(@publisher)
  6628.             IF @@error <> 0 
  6629.                 BEGIN
  6630.                     RETURN (1)
  6631.                 END
  6632.         END
  6633.  
  6634.     IF LOWER(@property) = 'password'
  6635.         BEGIN
  6636.             /*
  6637.             ** Set the Password registry key value
  6638.             */
  6639.             SELECT @new_password = CONVERT(sysname, @value)
  6640.  
  6641.             -- Encrypt the password
  6642.             EXEC @retcode = master.dbo.xp_repl_encrypt @new_password OUTPUT
  6643.             IF @@error <> 0 OR @retcode <> 0
  6644.                 RETURN (1)
  6645.  
  6646.             UPDATE msdb..MSdistpublishers SET password = @new_password
  6647.                 WHERE UPPER(name) = UPPER(@publisher)
  6648.             IF @@error <> 0 
  6649.                 BEGIN
  6650.                     RETURN (1)
  6651.                 END
  6652.          END
  6653.  
  6654.     IF LOWER(@property) = 'active'
  6655.         BEGIN
  6656.             /*
  6657.             ** Check for a valid  value.
  6658.             */
  6659.  
  6660.             IF LOWER(@value) NOT IN ('true', 'false')
  6661.             BEGIN
  6662.                 RAISERROR (14137, 16, -1)
  6663.                 RETURN (1)
  6664.             END
  6665.  
  6666.             IF LOWER(@value) = 'true'
  6667.             begin
  6668.                 -- Clean up the database in case of the remote publisher is reinstalling publishing.
  6669.                 SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  6670.                 exec @retcode = @command @publisher
  6671.                 if @retcode <> 0 or @@error <> 0
  6672.                     return 1
  6673.                 SELECT @new_active = 1
  6674.             end
  6675.             ELSE
  6676.             BEGIN
  6677.                 SELECT @new_active = 0
  6678.             END
  6679.  
  6680.             /*
  6681.             ** Set the Active registry key value
  6682.             */
  6683.             UPDATE msdb..MSdistpublishers SET active = @new_active
  6684.                 WHERE UPPER(name) = UPPER(@publisher)
  6685.             IF @@error <> 0
  6686.                 BEGIN
  6687.                     RETURN (1)
  6688.                 END
  6689.  
  6690.         END
  6691.  
  6692.     IF LOWER(@property) = 'trusted'
  6693.         BEGIN
  6694.             /*
  6695.             ** Check for a valid  value.
  6696.             */
  6697.  
  6698.             IF LOWER(@value) NOT IN ('true', 'false')
  6699.             BEGIN
  6700.                 RAISERROR (14137, 16, -1)
  6701.                 RETURN (1)
  6702.             END
  6703.  
  6704.             declare @fExists int
  6705.  
  6706.             IF LOWER(@value) = 'true'
  6707.             begin
  6708.                 SELECT @new_trusted = 1
  6709.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6710.                 if( @fExists = 1 )
  6711.                 BEGIN
  6712.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  6713.                     IF @@error <> 0 OR @retcode <> 0
  6714.                     BEGIN
  6715.                         RAISERROR (14075, 16, -1)
  6716.                         RETURN (1)
  6717.                     END
  6718.                 END
  6719.  
  6720.  
  6721.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 
  6722.                     'distributor_admin', trusted, true
  6723.                 IF @@error <> 0 OR @retcode <> 0
  6724.                 BEGIN
  6725.                     RAISERROR (14075, 16, -1)
  6726.                     RETURN (1)
  6727.                 END
  6728.             end
  6729.             ELSE
  6730.             BEGIN
  6731.                 SELECT @new_trusted = 0
  6732.      
  6733.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6734.                 if( @fExists = 1 )
  6735.                 BEGIN
  6736.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, false
  6737.                     IF @@error <> 0 OR @retcode <> 0
  6738.                     BEGIN
  6739.                         RAISERROR (14075, 16, -1)
  6740.                         RETURN (1)
  6741.                     END
  6742.                 END
  6743.  
  6744.  
  6745.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin',
  6746.                     'distributor_admin', trusted, 'false'
  6747.                 IF @@error <> 0 OR @retcode <> 0
  6748.                 BEGIN
  6749.                     RAISERROR (14075, 16, -1)
  6750.                     RETURN (1)
  6751.                 END
  6752.             END
  6753.  
  6754.             /*
  6755.             ** Set the trusted registry key value
  6756.             */
  6757.             UPDATE msdb..MSdistpublishers SET trusted = @new_trusted
  6758.                 WHERE UPPER(name) = UPPER(@publisher)
  6759.             IF @@error <> 0
  6760.                 BEGIN
  6761.                     RETURN (1)
  6762.                 END
  6763.  
  6764.         END
  6765.   
  6766.     /*
  6767.     ** Return succeed.
  6768.     */
  6769.     RAISERROR (21035, 10, -1, @property)
  6770.  
  6771. DONE:    
  6772.     RETURN (0)
  6773. go
  6774.  
  6775. raiserror('Creating procedure sp_helpdistpublisher', 0,1)
  6776. go
  6777.  
  6778. CREATE PROCEDURE sp_helpdistpublisher (
  6779.     @publisher sysname = '%'         /* publisher server name */
  6780.         ) AS
  6781.  
  6782.     SET NOCOUNT ON
  6783.  
  6784.     IF @publisher IS null
  6785.         begin
  6786.             raiserror (14043, 16, -1, '@publisher')
  6787.             return (1)
  6788.         end
  6789.  
  6790.     /*
  6791.     ** Check to make sure this is a distributor
  6792.     */
  6793.     IF NOT EXISTS (select * from master..sysservers
  6794.               where UPPER(datasource) = UPPER(@@SERVERNAME)
  6795.                  AND srvstatus & 8 <> 0)
  6796.     begin
  6797.         if @publisher <> '%' 
  6798.         begin
  6799.                 raiserror (14114, 16, -1, @@SERVERNAME)
  6800.                 return(1)
  6801.         end
  6802.         else
  6803.             return(0)
  6804.     end
  6805.  
  6806.     IF @publisher <> '%' AND NOT EXISTS (select * from msdb..MSdistpublishers
  6807.             where UPPER(name) = UPPER(@publisher))
  6808.     begin
  6809.         raiserror (14080, 11, -1)
  6810.         return (1)
  6811.     end
  6812.  
  6813.     SELECT name, distribution_db, security_mode, login,
  6814.         -- Not to return password unless sysadmin is called.
  6815.         'password' = case  when is_srvrolemember ('sysadmin') = 1 then password
  6816.             else convert(sysname, NULL)
  6817.         end,
  6818.         active, working_directory, trusted, thirdparty_flag
  6819.         FROM msdb.dbo.MSdistpublishers 
  6820.         where (@publisher = N'%') or (UPPER(name) = UPPER(@publisher))
  6821.     IF @@ERROR <> 0
  6822.     begin
  6823.         return(1)
  6824.     end
  6825. GO
  6826.  
  6827. dump tran master with no_log
  6828. go
  6829.  
  6830. raiserror('Creating procedure sp_add_agent_profile', 0,1)
  6831. go
  6832.  
  6833. /*
  6834. ** The system profile of the same type of agent will be used as a template for 
  6835. ** the parameters in this new user profile.
  6836. */
  6837. CREATE PROCEDURE sp_add_agent_profile (
  6838.     @profile_id             int = 0 OUTPUT,
  6839.     @profile_name           sysname,
  6840.     @agent_type             int,            -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  6841.     @profile_type           int = 0,        -- 0-System, 1-Custom 
  6842.     @description            nvarchar(3000),
  6843.     @default                bit = 0         -- 0-Not Default, 1-Default
  6844. )
  6845. AS
  6846.     SET NOCOUNT ON
  6847.  
  6848.     declare     @sys_profile    int
  6849.     declare     @default_id     int
  6850.     declare     @retcode        int
  6851.  
  6852.     SELECT @profile_name = RTRIM(@profile_name)
  6853.  
  6854.     IF @profile_name IS NULL
  6855.     BEGIN
  6856.         RAISERROR (14043, 16, -1, '@profile_name')
  6857.         RETURN (1)
  6858.     END
  6859.  
  6860.     exec @retcode = dbo.sp_MSreplcheck_name @profile_name
  6861.     if @@ERROR <> 0 or @retcode <> 0
  6862.         return(1)
  6863.  
  6864.     /* The profile name is unique across a particular agent type */
  6865.     IF EXISTS ( SELECT * FROM msdb..MSagent_profiles 
  6866.             WHERE profile_name = @profile_name
  6867.             AND agent_type = @agent_type )
  6868.     BEGIN
  6869.         RAISERROR(20057, 16, -1, @profile_name)
  6870.         RETURN (1)
  6871.     END
  6872.  
  6873.     IF @agent_type NOT IN (1, 2, 3, 4)
  6874.     BEGIN
  6875.         RAISERROR(20058, 16, -1)
  6876.         return (1)
  6877.     END
  6878.  
  6879.     IF @profile_type NOT IN (0, 1)
  6880.     BEGIN
  6881.         RAISERROR(20059, 16, -1)
  6882.         return (1)
  6883.     END
  6884.  
  6885.     BEGIN TRAN
  6886.  
  6887.     /*
  6888.     ** First find out the default profile of the same agent type
  6889.     */
  6890.     select @default_id = profile_id from msdb..MSagent_profiles WHERE agent_type = @agent_type AND def_profile = 1
  6891.     
  6892.     /* Only one profile for an agent type must be default */
  6893.     IF @default = 1
  6894.         UPDATE msdb..MSagent_profiles
  6895.             SET def_profile = 0 WHERE profile_id = @default_id
  6896.     IF @@ERROR <> 0
  6897.         GOTO UNDO
  6898.         
  6899.     INSERT INTO msdb..MSagent_profiles
  6900.             VALUES (@profile_name, @agent_type, @profile_type, @description, @default)
  6901.     IF @@ERROR <> 0
  6902.         GOTO UNDO
  6903.  
  6904.     IF @profile_id IS NULL
  6905.         SELECT @profile_id = profile_id 
  6906.         FROM msdb..MSagent_profiles
  6907.         WHERE profile_name = @profile_name 
  6908.         AND agent_type = @agent_type
  6909.  
  6910.     /*
  6911.     ** If there is system profiles of the same agent type, use the primary system one as template of parameter set
  6912.     */
  6913.     IF EXISTS (select * from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0)
  6914.         select @default_id = min(profile_id) from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0
  6915.  
  6916.     if @profile_type=1
  6917.     BEGIN
  6918.             INSERT INTO msdb..MSagent_parameters 
  6919.             select @profile_id, parameter_name, value from msdb..MSagent_parameters 
  6920.                 where profile_id = @default_id      
  6921.         if @@ERROR<>0 RETURN 1
  6922.     END
  6923.  
  6924.     COMMIT TRAN
  6925.     RETURN 0 
  6926.  
  6927. UNDO:
  6928.     IF @@TRANCOUNT = 1
  6929.         ROLLBACK TRAN
  6930.     ELSE
  6931.         COMMIT TRAN
  6932.  
  6933.     RETURN 1 
  6934. GO
  6935.  
  6936. raiserror('Creating procedure sp_drop_agent_parameter', 0,1)
  6937. go
  6938.  
  6939. -- Drop a/all parameter(s) of a/all profile(s) from the MSagent_parameters table
  6940. create procedure sp_drop_agent_parameter (
  6941.     @profile_id int,
  6942.     @parameter_name     sysname = '%'
  6943. )
  6944. as
  6945.     set nocount on
  6946.  
  6947.     declare @default            bit
  6948.     declare @agent_type         int
  6949.     declare @retstatus          int
  6950.     
  6951.     if @parameter_name is null
  6952.     BEGIN
  6953.         RAISERROR (14043, 16, -1, '@parameter_name')
  6954.         RETURN (1)
  6955.     END
  6956.  
  6957.     select @agent_type = agent_type, @default=def_profile
  6958.     from msdb..MSagent_profiles
  6959.     where profile_id = @profile_id
  6960.  
  6961.     if @default is null
  6962.     BEGIN
  6963.         RAISERROR (20066, 16, -1) -- profile not defined
  6964.         RETURN (1)
  6965.     END
  6966.  
  6967.     /* Delete the parameters of the profile */
  6968.     delete  msdb..MSagent_parameters 
  6969.     where   parameter_name like @parameter_name
  6970.     and     profile_id = @profile_id
  6971.  
  6972.     if @@error <> 0
  6973.     begin
  6974.         select @retstatus = 1
  6975.         goto UNDO
  6976.     end
  6977.  
  6978.     select @retstatus = 0
  6979.  
  6980. UNDO:
  6981.     
  6982.     return @retstatus
  6983. GO
  6984.  
  6985. raiserror('Creating procedure sp_drop_agent_profile', 0,1)
  6986. go
  6987. -- Drop a profile from the MSagent_profiles table, as well as the corresponding
  6988. -- parameters from the MSagent_parameters table
  6989.  
  6990. CREATE PROCEDURE sp_drop_agent_profile (
  6991.     @profile_id int
  6992. )
  6993. AS
  6994.     SET NOCOUNT ON
  6995.  
  6996.     declare @snapshot_type      int
  6997.     declare @logreader_type     int
  6998.     declare @distribution_type  int
  6999.     declare @merge_type         int
  7000.     
  7001.     declare @tablename          nvarchar(255)
  7002.     declare @proc               nvarchar(255)
  7003.     declare @distribution_db    sysname
  7004.     declare @profile_type       int
  7005.  
  7006.     declare @default            bit
  7007.     declare @usage_count        int
  7008.     declare @agent_type         int
  7009.     declare @retstatus          int
  7010.     DECLARE @retcode            int
  7011.  
  7012.     declare @default_sys_id     int
  7013.  
  7014.     SELECT @agent_type = agent_type, @profile_type = type, @default=def_profile
  7015.     FROM msdb..MSagent_profiles
  7016.     WHERE profile_id = @profile_id
  7017.  
  7018.     IF @default IS NULL
  7019.     BEGIN
  7020.         RAISERROR (20066, 16, -1) -- Profile not defined
  7021.         RETURN (1)
  7022.     END
  7023.  
  7024.     /*
  7025.     ** Before dropping a default profile, one system profile of the same agent type
  7026.     ** has to become the new default profile.
  7027.     */
  7028.     IF @default = 1
  7029.     BEGIN
  7030.         select @default_sys_id=min(profile_id) from msdb..MSagent_profiles
  7031.             where agent_type = @agent_type AND type = 0
  7032.             
  7033.         UPDATE msdb..MSagent_profiles SET def_profile = 1 WHERE profile_id = @default_sys_id
  7034.     END
  7035.  
  7036.     select @snapshot_type = 1
  7037.     select @logreader_type = 2
  7038.     select @distribution_type = 3
  7039.     select @merge_type = 4
  7040.  
  7041.         /* By default, assume that this profile is not being used */
  7042.     select @usage_count = -1
  7043.  
  7044.     select @tablename = 
  7045.         case @agent_type
  7046.             when @snapshot_type then 'MSsnapshot_agents'
  7047.             when @logreader_type then 'MSlogreader_agents'
  7048.             when @distribution_type then 'MSdistribution_agents'
  7049.             when @merge_type then 'MSmerge_agents'
  7050.         end
  7051.  
  7052.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD FOR
  7053.         select distinct distribution_db 
  7054.         from msdb..MSdistpublishers
  7055.         for read only
  7056.  
  7057.     open hCdistdbs
  7058.     fetch hCdistdbs into @distribution_db
  7059.  
  7060.     while @@fetch_status <> -1 and @usage_count = -1
  7061.     begin
  7062.         select @proc = @distribution_db + '.dbo.sp_MSprofile_in_use'
  7063.         execute @usage_count = @proc @tablename = @tablename, @profile_id = @profile_id
  7064.     
  7065.         if @@error <> 0
  7066.         begin
  7067.             select @retstatus = 1
  7068.             goto UNDO
  7069.         end
  7070.  
  7071.         fetch hCdistdbs into @distribution_db
  7072.     end
  7073.  
  7074.     /* A profile in use cannot be dropped */
  7075.     if @usage_count = 0 
  7076.     begin
  7077.         RAISERROR(20065, 16, -1) -- Cannot drop profile, because it is in use.
  7078.         select @retstatus = 1
  7079.         goto UNDO 
  7080.     end
  7081.  
  7082.     BEGIN TRAN
  7083.  
  7084.     /*****
  7085.      * NOTE : If sp_drop_agent_parameter fails, the profile must not be 
  7086.      * deleted either 
  7087.      */
  7088.     EXECUTE @retcode = dbo.sp_drop_agent_parameter @profile_id = @profile_id,
  7089.                             @parameter_name = '%'
  7090.  
  7091.     IF @@ERROR <> 0 OR @retcode <> 0
  7092.         GOTO UNDO
  7093.             
  7094.     /* Delete all the entries in one go */
  7095.     DELETE msdb..MSagent_profiles 
  7096.     WHERE profile_id = @profile_id
  7097.  
  7098.     IF @@ERROR <> 0
  7099.         GOTO UNDO
  7100.  
  7101.     COMMIT TRAN
  7102.  
  7103.     close hCdistdbs
  7104.     deallocate hCdistdbs
  7105.  
  7106.     RETURN 0
  7107.  
  7108. UNDO:
  7109.     IF @@TRANCOUNT = 1
  7110.         ROLLBACK TRAN
  7111.     ELSE IF @@TRANCOUNT > 1 -- Sometimes we can get here when @@trancount = 0, so need to check explicitly.
  7112.         COMMIT TRAN
  7113.  
  7114.     close hCdistdbs
  7115.     deallocate hCdistdbs
  7116.  
  7117.  
  7118.     RETURN (1)
  7119. GO
  7120.  
  7121. raiserror('Creating procedure sp_help_agent_profile', 0,1)
  7122. go
  7123.  
  7124.  
  7125. -- View the row(s) of the MSagent_profiles table
  7126.  
  7127. create procedure sp_help_agent_profile(
  7128.     @agent_type     int = 0,
  7129.     @profile_id int = -1
  7130. )
  7131. as
  7132.     set nocount on
  7133.  
  7134.     declare @proc nvarchar(255)
  7135.  
  7136.     declare @snapshot_type      int
  7137.     declare @logreader_type     int
  7138.     declare @distribution_type  int
  7139.     declare @merge_type     int
  7140.  
  7141.     select @snapshot_type = 1
  7142.     select @logreader_type = 2
  7143.     select @distribution_type = 3
  7144.     select @merge_type = 4
  7145.  
  7146.     if @agent_type is null 
  7147.     BEGIN
  7148.         RAISERROR (14043, 16, -1, '@agent_type')
  7149.         RETURN (1)
  7150.     END
  7151.  
  7152.     if @profile_id is null
  7153.     BEGIN
  7154.         RAISERROR (14043, 16, -1, '@profile_id')
  7155.         RETURN (1)
  7156.     END
  7157.  
  7158.     if @agent_type not in (0, @snapshot_type, @logreader_type, @distribution_type, @merge_type)
  7159.     BEGIN
  7160.         RAISERROR(20058, 16, -1)
  7161.         return (1)
  7162.     END
  7163.  
  7164.  
  7165.     select @proc =  ' select profile_id, profile_name, agent_type, type, ' +
  7166.             ' description, def_profile ' +
  7167.             ' from msdb..MSagent_profiles ' +
  7168.             case 
  7169.               when @profile_id <> -1 then ' where profile_id = ' + convert(nvarchar(10),@profile_id) else ' '
  7170.             end
  7171.  
  7172.     if @agent_type <> 0 
  7173.         if @profile_id = -1
  7174.             select @proc = @proc + ' where '
  7175.         else 
  7176.             select @proc = @proc + ' and '
  7177.  
  7178.     select @proc = @proc + 
  7179.             case 
  7180.               when @agent_type <> 0 then ' agent_type = ' + convert(nvarchar(10),@agent_type) else ' '
  7181.             end
  7182.     
  7183.     select @proc = @proc + ' order by profile_id, profile_name'
  7184.  
  7185.     execute (@proc)
  7186. GO
  7187.  
  7188. raiserror('Creating procedure sp_help_agent_default', 0,1)
  7189. go
  7190.  
  7191. create procedure sp_help_agent_default (
  7192.     @profile_id     int OUTPUT, 
  7193.     @agent_type         int
  7194. )
  7195. as
  7196.     set nocount on
  7197.  
  7198.     if @agent_type not in (1, 2, 3, 4)
  7199.     BEGIN
  7200.         RAISERROR(20058, 16, -1)
  7201.         return (1)
  7202.     END
  7203.  
  7204.  
  7205.     select @profile_id = profile_id 
  7206.     from msdb..MSagent_profiles 
  7207.     where agent_type = @agent_type
  7208.     and def_profile = 1 
  7209. go
  7210.  
  7211. raiserror('Creating procedure sp_MSupdate_agenttype_default', 0,1)
  7212. go
  7213.  
  7214. -- Set a profile as a default for an agent_type
  7215. create procedure sp_MSupdate_agenttype_default(
  7216.     @profile_id int
  7217. )
  7218. as
  7219.     set nocount on
  7220.  
  7221.     declare @agent_type int
  7222.  
  7223.     /* This profile must have been defined for this agent type */
  7224.     if not exists ( select * from msdb..MSagent_profiles
  7225.             where profile_id = @profile_id )
  7226.     BEGIN
  7227.         RAISERROR (20066, 16, -1)   -- profile not defined
  7228.         RETURN (1)
  7229.     END
  7230.  
  7231.  
  7232.     select @agent_type = agent_type
  7233.     from msdb..MSagent_profiles 
  7234.     where profile_id = @profile_id
  7235.  
  7236.     BEGIN TRAN
  7237.  
  7238.     update msdb..MSagent_profiles
  7239.     set def_profile = 0
  7240.     where agent_type = @agent_type
  7241.     and def_profile = 1
  7242.  
  7243.     if @@error <> 0
  7244.         goto UNDO
  7245.  
  7246.     update msdb..MSagent_profiles
  7247.     set def_profile = 1
  7248.     where profile_id = @profile_id
  7249.  
  7250.     if @@error <> 0
  7251.         goto UNDO 
  7252.  
  7253.     COMMIT TRAN
  7254.  
  7255.     return 0
  7256.  
  7257. UNDO:
  7258.     if @@trancount = 1
  7259.         rollback tran
  7260.     else
  7261.         commit tran
  7262.  
  7263.     return 1 
  7264. GO
  7265.  
  7266.  
  7267. /*
  7268. ** Create stored procedures to generate default profile 
  7269. ** parameters for replication agents.
  7270. */
  7271.  
  7272. raiserror('Creating procedure sp_generate_agent_parameter', 0,1)
  7273. GO
  7274.  
  7275. create procedure sp_generate_agent_parameter(
  7276.     @profile_id     int
  7277. )
  7278. as
  7279.     declare @retcode    int
  7280.  
  7281.      
  7282.     /* 
  7283.     ** Snapshot agent 
  7284.     */
  7285.     if (@profile_id = 1)
  7286.       begin
  7287.         exec @retcode = dbo.sp_add_agent_parameter 
  7288.                 @profile_id = @profile_id,
  7289.                 @parameter_name = '-BcpBatchSize',
  7290.                 @parameter_value = '100000'
  7291.         if (@retcode = 1 or @@ERROR <> 0)
  7292.             goto FAILURE
  7293.  
  7294.         exec @retcode = dbo.sp_add_agent_parameter 
  7295.                 @profile_id = @profile_id,
  7296.                 @parameter_name = '-HistoryVerboseLevel',
  7297.                 @parameter_value = '2'
  7298.         if (@retcode = 1 or @@ERROR <> 0)
  7299.             goto FAILURE
  7300.  
  7301.         exec @retcode = dbo.sp_add_agent_parameter 
  7302.                 @profile_id = @profile_id,
  7303.                 @parameter_name = '-LoginTimeout',
  7304.                 @parameter_value = '15'
  7305.         if (@retcode = 1 or @@ERROR <> 0)
  7306.             goto FAILURE
  7307.  
  7308.         exec @retcode = dbo.sp_add_agent_parameter 
  7309.                 @profile_id = @profile_id,
  7310.                 @parameter_name = '-MaxBcpThreads',
  7311.                 @parameter_value = '1'
  7312.         if (@retcode = 1 or @@ERROR <> 0)
  7313.             goto FAILURE
  7314.  
  7315.         exec @retcode = dbo.sp_add_agent_parameter 
  7316.                 @profile_id = @profile_id,
  7317.                 @parameter_name = '-QueryTimeout',
  7318.                 @parameter_value = '300'        -- 5 minutes 
  7319.         if (@retcode = 1 or @@ERROR <> 0)
  7320.             goto FAILURE
  7321.  
  7322.       end
  7323.  
  7324.     /* 
  7325.     ** Logreader agent 
  7326.     */
  7327.     else if (@profile_id = 2)
  7328.       begin
  7329.   
  7330.         exec @retcode = dbo.sp_add_agent_parameter 
  7331.                 @profile_id = @profile_id,
  7332.                 @parameter_name = '-HistoryVerboseLevel',
  7333.                 @parameter_value = '1'
  7334.         if (@retcode = 1 or @@ERROR <> 0)
  7335.             goto FAILURE
  7336.  
  7337.         exec @retcode = dbo.sp_add_agent_parameter 
  7338.                 @profile_id = @profile_id,
  7339.                 @parameter_name = '-LoginTimeout',
  7340.                 @parameter_value = '15'
  7341.         if (@retcode = 1 or @@ERROR <> 0)
  7342.             goto FAILURE
  7343.  
  7344.         exec @retcode = dbo.sp_add_agent_parameter 
  7345.                 @profile_id = @profile_id,
  7346.                 @parameter_name = '-PollingInterval',
  7347.                 @parameter_value = '10'
  7348.         if (@retcode = 1 or @@ERROR <> 0)
  7349.             goto FAILURE
  7350.  
  7351.         exec @retcode = dbo.sp_add_agent_parameter 
  7352.                 @profile_id = @profile_id,
  7353.                 @parameter_name = '-QueryTimeout',
  7354.                 @parameter_value = '300'        -- 5 minutes
  7355.         if (@retcode = 1 or @@ERROR <> 0)
  7356.             goto FAILURE
  7357.  
  7358.         exec @retcode = dbo.sp_add_agent_parameter 
  7359.                 @profile_id = @profile_id,
  7360.                 @parameter_name = '-ReadBatchSize',
  7361.                 @parameter_value = '500'
  7362.         if (@retcode = 1 or @@ERROR <> 0)
  7363.             goto FAILURE
  7364.  
  7365.         exec @retcode = dbo.sp_add_agent_parameter 
  7366.                 @profile_id = @profile_id,
  7367.                 @parameter_name = '-ReadBatchThreshold',
  7368.                 @parameter_value = '100'
  7369.         if (@retcode = 1 or @@ERROR <> 0)
  7370.             goto FAILURE
  7371.  
  7372.       end
  7373.  
  7374.     /* 
  7375.     ** Logreader agent - Verbose History Profile
  7376.     */
  7377.     else if (@profile_id = 3)
  7378.       begin
  7379.  
  7380.         exec @retcode = dbo.sp_add_agent_parameter 
  7381.                 @profile_id = @profile_id,
  7382.                 @parameter_name = '-HistoryVerboseLevel',
  7383.                 @parameter_value = '2'
  7384.         if (@retcode = 1 or @@ERROR <> 0)
  7385.             goto FAILURE
  7386.  
  7387.         exec @retcode = dbo.sp_add_agent_parameter 
  7388.                 @profile_id = @profile_id,
  7389.                 @parameter_name = '-LoginTimeout',
  7390.                 @parameter_value = '15'
  7391.         if (@retcode = 1 or @@ERROR <> 0)
  7392.             goto FAILURE
  7393.  
  7394.         exec @retcode = dbo.sp_add_agent_parameter 
  7395.                 @profile_id = @profile_id,
  7396.                 @parameter_name = '-PollingInterval',
  7397.                 @parameter_value = '10'
  7398.         if (@retcode = 1 or @@ERROR <> 0)
  7399.             goto FAILURE
  7400.  
  7401.         exec @retcode = dbo.sp_add_agent_parameter 
  7402.                 @profile_id = @profile_id,
  7403.                 @parameter_name = '-QueryTimeout',
  7404.                 @parameter_value = '300'        -- 5 minutes
  7405.         if (@retcode = 1 or @@ERROR <> 0)
  7406.             goto FAILURE
  7407.  
  7408.         exec @retcode = dbo.sp_add_agent_parameter 
  7409.                 @profile_id = @profile_id,
  7410.                 @parameter_name = '-ReadBatchSize',
  7411.                 @parameter_value = '500'
  7412.         if (@retcode = 1 or @@ERROR <> 0)
  7413.             goto FAILURE
  7414.  
  7415.         exec @retcode = dbo.sp_add_agent_parameter 
  7416.                 @profile_id = @profile_id,
  7417.                 @parameter_name = '-ReadBatchThreshold',
  7418.                 @parameter_value = '100'
  7419.         if (@retcode = 1 or @@ERROR <> 0)
  7420.             goto FAILURE
  7421.       end
  7422.  
  7423.     /* 
  7424.     ** Distribution agent 
  7425.     */
  7426.     else if (@profile_id = 4)
  7427.       begin
  7428.  
  7429.         exec @retcode = dbo.sp_add_agent_parameter 
  7430.                 @profile_id = @profile_id,
  7431.                 @parameter_name = '-BcpBatchSize',
  7432.                 @parameter_value = '100000'
  7433.         if (@retcode = 1 or @@ERROR <> 0)
  7434.             goto FAILURE
  7435.  
  7436.         exec @retcode = dbo.sp_add_agent_parameter 
  7437.                 @profile_id = @profile_id,
  7438.                 @parameter_name = '-CommitBatchSize',
  7439.                 @parameter_value = '100'
  7440.         if (@retcode = 1 or @@ERROR <> 0)
  7441.             goto FAILURE
  7442.  
  7443.         exec @retcode = dbo.sp_add_agent_parameter 
  7444.                 @profile_id = @profile_id,
  7445.                 @parameter_name = '-CommitBatchThreshold',
  7446.                 @parameter_value = '1000'
  7447.         if (@retcode = 1 or @@ERROR <> 0)
  7448.             goto FAILURE
  7449.  
  7450.         exec @retcode = dbo.sp_add_agent_parameter 
  7451.                 @profile_id = @profile_id,
  7452.                 @parameter_name = '-HistoryVerboseLevel',
  7453.                 @parameter_value = '1'
  7454.         if (@retcode = 1 or @@ERROR <> 0)
  7455.             goto FAILURE
  7456.  
  7457.         exec @retcode = dbo.sp_add_agent_parameter 
  7458.                 @profile_id = @profile_id,
  7459.                 @parameter_name = '-LoginTimeout',
  7460.                 @parameter_value = '15'
  7461.         if (@retcode = 1 or @@ERROR <> 0)
  7462.             goto FAILURE
  7463.  
  7464.         exec @retcode = dbo.sp_add_agent_parameter 
  7465.                 @profile_id = @profile_id,
  7466.                 @parameter_name = '-MaxBcpThreads',
  7467.                 @parameter_value = '1'
  7468.         if (@retcode = 1 or @@ERROR <> 0)
  7469.             goto FAILURE
  7470.  
  7471.         exec @retcode = dbo.sp_add_agent_parameter 
  7472.                 @profile_id = @profile_id,
  7473.                 @parameter_name = '-MaxDeliveredTransactions',
  7474.                 @parameter_value = '0'
  7475.         if (@retcode = 1 or @@ERROR <> 0)
  7476.             goto FAILURE
  7477.  
  7478.         exec @retcode = dbo.sp_add_agent_parameter 
  7479.                 @profile_id = @profile_id,
  7480.                 @parameter_name = '-PollingInterval',
  7481.                 @parameter_value = '10'
  7482.         if (@retcode = 1 or @@ERROR <> 0)
  7483.             goto FAILURE
  7484.  
  7485.         exec @retcode = dbo.sp_add_agent_parameter 
  7486.                 @profile_id = @profile_id,
  7487.                 @parameter_name = '-QueryTimeout',
  7488.                 @parameter_value = '300'        -- 5 minutes 
  7489.         if (@retcode = 1 or @@ERROR <> 0)
  7490.             goto FAILURE
  7491.  
  7492.         exec @retcode = dbo.sp_add_agent_parameter 
  7493.                 @profile_id = @profile_id,
  7494.                 @parameter_name = '-SkipFailureLevel',
  7495.                 @parameter_value = '1'
  7496.         if (@retcode = 1 or @@ERROR <> 0)
  7497.             goto FAILURE
  7498.  
  7499.         exec @retcode = dbo.sp_add_agent_parameter 
  7500.                 @profile_id = @profile_id,
  7501.                 @parameter_name = '-TransactionsPerHistory',
  7502.                 @parameter_value = '100'
  7503.         if (@retcode = 1 or @@ERROR <> 0)
  7504.             goto FAILURE
  7505.  
  7506.       end
  7507.  
  7508.     /*
  7509.     ** Distribution Agent Verbose History Profile
  7510.     */
  7511.     else if (@profile_id = 5)
  7512.       begin
  7513.  
  7514.         exec @retcode = dbo.sp_add_agent_parameter 
  7515.                 @profile_id = @profile_id,
  7516.                 @parameter_name = '-BcpBatchSize',
  7517.                 @parameter_value = '100000'
  7518.         if (@retcode = 1 or @@ERROR <> 0)
  7519.             goto FAILURE
  7520.  
  7521.         exec @retcode = dbo.sp_add_agent_parameter 
  7522.                 @profile_id = @profile_id,
  7523.                 @parameter_name = '-CommitBatchSize',
  7524.                 @parameter_value = '100'
  7525.         if (@retcode = 1 or @@ERROR <> 0)
  7526.             goto FAILURE
  7527.  
  7528.         exec @retcode = dbo.sp_add_agent_parameter 
  7529.                 @profile_id = @profile_id,
  7530.                 @parameter_name = '-CommitBatchThreshold',
  7531.                 @parameter_value = '1000'
  7532.         if (@retcode = 1 or @@ERROR <> 0)
  7533.             goto FAILURE
  7534.  
  7535.         exec @retcode = dbo.sp_add_agent_parameter 
  7536.                 @profile_id = @profile_id,
  7537.                 @parameter_name = '-HistoryVerboseLevel',
  7538.                 @parameter_value = '2'
  7539.         if (@retcode = 1 or @@ERROR <> 0)
  7540.             goto FAILURE
  7541.  
  7542.         exec @retcode = dbo.sp_add_agent_parameter 
  7543.                 @profile_id = @profile_id,
  7544.                 @parameter_name = '-LoginTimeout',
  7545.                 @parameter_value = '15'
  7546.         if (@retcode = 1 or @@ERROR <> 0)
  7547.             goto FAILURE
  7548.  
  7549.         exec @retcode = dbo.sp_add_agent_parameter 
  7550.                 @profile_id = @profile_id,
  7551.                 @parameter_name = '-MaxBcpThreads',
  7552.                 @parameter_value = '1'
  7553.         if (@retcode = 1 or @@ERROR <> 0)
  7554.             goto FAILURE
  7555.  
  7556.         exec @retcode = dbo.sp_add_agent_parameter 
  7557.                 @profile_id = @profile_id,
  7558.                 @parameter_name = '-MaxDeliveredTransactions',
  7559.                 @parameter_value = '0'
  7560.         if (@retcode = 1 or @@ERROR <> 0)
  7561.             goto FAILURE
  7562.  
  7563.         exec @retcode = dbo.sp_add_agent_parameter 
  7564.                 @profile_id = @profile_id,
  7565.                 @parameter_name = '-PollingInterval',
  7566.                 @parameter_value = '10'
  7567.         if (@retcode = 1 or @@ERROR <> 0)
  7568.             goto FAILURE
  7569.  
  7570.         exec @retcode = dbo.sp_add_agent_parameter 
  7571.                 @profile_id = @profile_id,
  7572.                 @parameter_name = '-QueryTimeout',
  7573.                 @parameter_value = '300'        -- 5 minutes 
  7574.         if (@retcode = 1 or @@ERROR <> 0)
  7575.             goto FAILURE
  7576.  
  7577.         exec @retcode = dbo.sp_add_agent_parameter 
  7578.                 @profile_id = @profile_id,
  7579.                 @parameter_name = '-SkipFailureLevel',
  7580.                 @parameter_value = '1'
  7581.         if (@retcode = 1 or @@ERROR <> 0)
  7582.             goto FAILURE
  7583.  
  7584.         exec @retcode = dbo.sp_add_agent_parameter 
  7585.                 @profile_id = @profile_id,
  7586.                 @parameter_name = '-TransactionsPerHistory',
  7587.                 @parameter_value = '100'
  7588.         if (@retcode = 1 or @@ERROR <> 0)
  7589.             goto FAILURE
  7590.  
  7591.       end
  7592.     /* 
  7593.     ** Merge agent : Default profile for well connected scenarios 
  7594.     */
  7595.     else if (@profile_id = 6)
  7596.       begin
  7597.  
  7598.         exec @retcode = dbo.sp_add_agent_parameter 
  7599.                 @profile_id = @profile_id,
  7600.                 @parameter_name = '-PollingInterval',
  7601.                 @parameter_value = '60'
  7602.         if (@retcode = 1 or @@ERROR <> 0)
  7603.             goto FAILURE
  7604.  
  7605.         exec @retcode = dbo.sp_add_agent_parameter 
  7606.                 @profile_id = @profile_id,
  7607.                 @parameter_name = '-ValidateInterval',
  7608.                 @parameter_value = '60'
  7609.         if (@retcode = 1 or @@ERROR <> 0)
  7610.             goto FAILURE
  7611.  
  7612.         exec @retcode = dbo.sp_add_agent_parameter 
  7613.                 @profile_id = @profile_id,
  7614.                 @parameter_name = '-LoginTimeout',
  7615.                 @parameter_value = '15'
  7616.         if (@retcode = 1 or @@ERROR <> 0)
  7617.             goto FAILURE
  7618.  
  7619.         exec @retcode = dbo.sp_add_agent_parameter 
  7620.                 @profile_id = @profile_id,
  7621.                 @parameter_name = '-QueryTimeout',
  7622.                 @parameter_value = '300'        
  7623.         if (@retcode = 1 or @@ERROR <> 0)
  7624.             goto FAILURE
  7625.  
  7626.         exec @retcode = dbo.sp_add_agent_parameter 
  7627.                 @profile_id = @profile_id,
  7628.                 @parameter_name = '-UploadGenerationsPerBatch',
  7629.                 @parameter_value = '100'
  7630.         if (@retcode = 1 or @@ERROR <> 0)
  7631.             goto FAILURE
  7632.  
  7633.         exec @retcode = dbo.sp_add_agent_parameter 
  7634.                 @profile_id = @profile_id,
  7635.                 @parameter_name = '-DownloadGenerationsPerBatch',
  7636.                 @parameter_value = '100'
  7637.         if (@retcode = 1 or @@ERROR <> 0)
  7638.             goto FAILURE
  7639.  
  7640.         exec @retcode = dbo.sp_add_agent_parameter 
  7641.                 @profile_id = @profile_id,
  7642.                 @parameter_name = '-UploadReadChangesPerBatch',
  7643.                 @parameter_value = '100'
  7644.         if (@retcode = 1 or @@ERROR <> 0)
  7645.             goto FAILURE
  7646.  
  7647.         exec @retcode = dbo.sp_add_agent_parameter 
  7648.                 @profile_id = @profile_id,
  7649.                 @parameter_name = '-DownloadReadChangesPerBatch',
  7650.                 @parameter_value = '100'
  7651.         if (@retcode = 1 or @@ERROR <> 0)
  7652.             goto FAILURE
  7653.  
  7654.         exec @retcode = dbo.sp_add_agent_parameter 
  7655.                 @profile_id = @profile_id,
  7656.                 @parameter_name = '-UploadWriteChangesPerBatch',
  7657.                 @parameter_value = '100'
  7658.         if (@retcode = 1 or @@ERROR <> 0)
  7659.             goto FAILURE
  7660.  
  7661.         exec @retcode = dbo.sp_add_agent_parameter 
  7662.                 @profile_id = @profile_id,
  7663.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  7664.                 @parameter_value = '100'
  7665.         if (@retcode = 1 or @@ERROR <> 0)
  7666.             goto FAILURE
  7667.  
  7668.         exec @retcode = dbo.sp_add_agent_parameter 
  7669.                 @profile_id = @profile_id,
  7670.                 @parameter_name = '-Validate',
  7671.                 @parameter_value = '0'
  7672.         if (@retcode = 1 or @@ERROR <> 0)
  7673.             goto FAILURE
  7674.  
  7675.         exec @retcode = dbo.sp_add_agent_parameter 
  7676.                 @profile_id = @profile_id,
  7677.                 @parameter_name = '-FastRowCount',
  7678.                 @parameter_value = '1'
  7679.         if (@retcode = 1 or @@ERROR <> 0)
  7680.             goto FAILURE
  7681.         
  7682.         exec @retcode = dbo.sp_add_agent_parameter 
  7683.                 @profile_id = @profile_id,
  7684.                 @parameter_name = '-HistoryVerboseLevel',
  7685.                 @parameter_value = '1'
  7686.         if (@retcode = 1 or @@ERROR <> 0)
  7687.             goto FAILURE
  7688.  
  7689.         exec @retcode = dbo.sp_add_agent_parameter 
  7690.                 @profile_id = @profile_id,
  7691.                 @parameter_name = '-ChangesPerHistory',
  7692.                 @parameter_value = '100'
  7693.         if (@retcode = 1 or @@ERROR <> 0)
  7694.             goto FAILURE
  7695.         exec @retcode = dbo.sp_add_agent_parameter 
  7696.                 @profile_id = @profile_id,
  7697.                 @parameter_name = '-BcpBatchSize',
  7698.                 @parameter_value = '100000'
  7699.         if (@retcode = 1 or @@ERROR <> 0)
  7700.             goto FAILURE
  7701.             
  7702.       end
  7703.  
  7704.     /* 
  7705.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  7706.     */
  7707.     else if (@profile_id = 7)
  7708.       begin
  7709.  
  7710.         exec @retcode = dbo.sp_add_agent_parameter 
  7711.                 @profile_id = @profile_id,
  7712.                 @parameter_name = '-PollingInterval',
  7713.                 @parameter_value = '60'
  7714.         if (@retcode = 1 or @@ERROR <> 0)
  7715.             goto FAILURE
  7716.  
  7717.         exec @retcode = dbo.sp_add_agent_parameter 
  7718.                 @profile_id = @profile_id,
  7719.                 @parameter_name = '-ValidateInterval',
  7720.                 @parameter_value = '60'
  7721.         if (@retcode = 1 or @@ERROR <> 0)
  7722.             goto FAILURE
  7723.  
  7724.         exec @retcode = dbo.sp_add_agent_parameter 
  7725.                 @profile_id = @profile_id,
  7726.                 @parameter_name = '-LoginTimeout',
  7727.                 @parameter_value = '15'
  7728.         if (@retcode = 1 or @@ERROR <> 0)
  7729.             goto FAILURE
  7730.  
  7731.         exec @retcode = dbo.sp_add_agent_parameter 
  7732.                 @profile_id = @profile_id,
  7733.                 @parameter_name = '-QueryTimeout',
  7734.                 @parameter_value = '300'
  7735.         if (@retcode = 1 or @@ERROR <> 0)
  7736.             goto FAILURE
  7737.  
  7738.         exec @retcode = dbo.sp_add_agent_parameter 
  7739.                 @profile_id = @profile_id,
  7740.                 @parameter_name = '-UploadGenerationsPerBatch',
  7741.                 @parameter_value = '1'
  7742.         if (@retcode = 1 or @@ERROR <> 0)
  7743.             goto FAILURE
  7744.  
  7745.         exec @retcode = dbo.sp_add_agent_parameter 
  7746.                 @profile_id = @profile_id,
  7747.                 @parameter_name = '-DownloadGenerationsPerBatch',
  7748.                 @parameter_value = '20'
  7749.         if (@retcode = 1 or @@ERROR <> 0)
  7750.             goto FAILURE
  7751.  
  7752.         exec @retcode = dbo.sp_add_agent_parameter 
  7753.                 @profile_id = @profile_id,
  7754.                 @parameter_name = '-UploadReadChangesPerBatch',
  7755.                 @parameter_value = '100'
  7756.         if (@retcode = 1 or @@ERROR <> 0)
  7757.             goto FAILURE
  7758.  
  7759.         exec @retcode = dbo.sp_add_agent_parameter 
  7760.                 @profile_id = @profile_id,
  7761.                 @parameter_name = '-DownloadReadChangesPerBatch',
  7762.                 @parameter_value = '100'
  7763.         if (@retcode = 1 or @@ERROR <> 0)
  7764.             goto FAILURE
  7765.  
  7766.         exec @retcode = dbo.sp_add_agent_parameter 
  7767.                 @profile_id = @profile_id,
  7768.                 @parameter_name = '-UploadWriteChangesPerBatch',
  7769.                 @parameter_value = '100'
  7770.         if (@retcode = 1 or @@ERROR <> 0)
  7771.             goto FAILURE
  7772.  
  7773.         exec @retcode = dbo.sp_add_agent_parameter 
  7774.                 @profile_id = @profile_id,
  7775.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  7776.                 @parameter_value = '100'
  7777.         if (@retcode = 1 or @@ERROR <> 0)
  7778.             goto FAILURE
  7779.  
  7780.         exec @retcode = dbo.sp_add_agent_parameter 
  7781.                 @profile_id = @profile_id,
  7782.                 @parameter_name = '-Validate',
  7783.                 @parameter_value = '0'
  7784.         if (@retcode = 1 or @@ERROR <> 0)
  7785.             goto FAILURE
  7786.         
  7787.         exec @retcode = dbo.sp_add_agent_parameter 
  7788.                 @profile_id = @profile_id,
  7789.                 @parameter_name = '-HistoryVerboseLevel',
  7790.                 @parameter_value = '1'
  7791.         if (@retcode = 1 or @@ERROR <> 0)
  7792.             goto FAILURE
  7793.  
  7794.         exec @retcode = dbo.sp_add_agent_parameter 
  7795.                 @profile_id = @profile_id,
  7796.                 @parameter_name = '-ChangesPerHistory',
  7797.                 @parameter_value = '100'
  7798.         if (@retcode = 1 or @@ERROR <> 0)
  7799.             goto FAILURE
  7800.         exec @retcode = dbo.sp_add_agent_parameter 
  7801.                 @profile_id = @profile_id,
  7802.                 @parameter_name = '-BcpBatchSize',
  7803.                 @parameter_value = '100000'
  7804.         if (@retcode = 1 or @@ERROR <> 0)
  7805.             goto FAILURE
  7806.       end
  7807.  
  7808.     /* 
  7809.     ** Merge agent : Default profile for well connected scenarios - Verbose history logging 
  7810.     */
  7811.     else if (@profile_id = 8)
  7812.       begin
  7813.  
  7814.         exec @retcode = dbo.sp_add_agent_parameter 
  7815.                 @profile_id = @profile_id,
  7816.                 @parameter_name = '-PollingInterval',
  7817.                 @parameter_value = '60'
  7818.         if (@retcode = 1 or @@ERROR <> 0)
  7819.             goto FAILURE
  7820.  
  7821.         exec @retcode = dbo.sp_add_agent_parameter 
  7822.                 @profile_id = @profile_id,
  7823.                 @parameter_name = '-ValidateInterval',
  7824.                 @parameter_value = '60'
  7825.         if (@retcode = 1 or @@ERROR <> 0)
  7826.             goto FAILURE
  7827.  
  7828.         exec @retcode = dbo.sp_add_agent_parameter 
  7829.                 @profile_id = @profile_id,
  7830.                 @parameter_name = '-LoginTimeout',
  7831.                 @parameter_value = '15'
  7832.         if (@retcode = 1 or @@ERROR <> 0)
  7833.             goto FAILURE
  7834.  
  7835.         exec @retcode = dbo.sp_add_agent_parameter 
  7836.                 @profile_id = @profile_id,
  7837.                 @parameter_name = '-QueryTimeout',
  7838.                 @parameter_value = '300'        
  7839.         if (@retcode = 1 or @@ERROR <> 0)
  7840.             goto FAILURE
  7841.  
  7842.         exec @retcode = dbo.sp_add_agent_parameter 
  7843.                 @profile_id = @profile_id,
  7844.                 @parameter_name = '-UploadGenerationsPerBatch',
  7845.                 @parameter_value = '100'
  7846.         if (@retcode = 1 or @@ERROR <> 0)
  7847.             goto FAILURE
  7848.  
  7849.         exec @retcode = dbo.sp_add_agent_parameter 
  7850.                 @profile_id = @profile_id,
  7851.                 @parameter_name = '-DownloadGenerationsPerBatch',
  7852.                 @parameter_value = '100'
  7853.         if (@retcode = 1 or @@ERROR <> 0)
  7854.             goto FAILURE
  7855.  
  7856.         exec @retcode = dbo.sp_add_agent_parameter 
  7857.                 @profile_id = @profile_id,
  7858.                 @parameter_name = '-UploadReadChangesPerBatch',
  7859.                 @parameter_value = '100'
  7860.         if (@retcode = 1 or @@ERROR <> 0)
  7861.             goto FAILURE
  7862.  
  7863.         exec @retcode = dbo.sp_add_agent_parameter 
  7864.                 @profile_id = @profile_id,
  7865.                 @parameter_name = '-DownloadReadChangesPerBatch',
  7866.                 @parameter_value = '100'
  7867.         if (@retcode = 1 or @@ERROR <> 0)
  7868.             goto FAILURE
  7869.  
  7870.         exec @retcode = dbo.sp_add_agent_parameter 
  7871.                 @profile_id = @profile_id,
  7872.                 @parameter_name = '-UploadWriteChangesPerBatch',
  7873.                 @parameter_value = '100'
  7874.         if (@retcode = 1 or @@ERROR <> 0)
  7875.             goto FAILURE
  7876.  
  7877.         exec @retcode = dbo.sp_add_agent_parameter 
  7878.                 @profile_id = @profile_id,
  7879.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  7880.                 @parameter_value = '100'
  7881.         if (@retcode = 1 or @@ERROR <> 0)
  7882.             goto FAILURE
  7883.  
  7884.         exec @retcode = dbo.sp_add_agent_parameter 
  7885.                 @profile_id = @profile_id,
  7886.                 @parameter_name = '-Validate',
  7887.                 @parameter_value = '0'
  7888.         if (@retcode = 1 or @@ERROR <> 0)
  7889.             goto FAILURE
  7890.  
  7891.         exec @retcode = dbo.sp_add_agent_parameter 
  7892.                 @profile_id = @profile_id,
  7893.                 @parameter_name = '-FastRowCount',
  7894.                 @parameter_value = '1'
  7895.         if (@retcode = 1 or @@ERROR <> 0)
  7896.             goto FAILURE
  7897.         
  7898.         exec @retcode = dbo.sp_add_agent_parameter 
  7899.                 @profile_id = @profile_id,
  7900.                 @parameter_name = '-HistoryVerboseLevel',
  7901.                 @parameter_value = '3'
  7902.         if (@retcode = 1 or @@ERROR <> 0)
  7903.             goto FAILURE
  7904.  
  7905.         exec @retcode = dbo.sp_add_agent_parameter 
  7906.                 @profile_id = @profile_id,
  7907.                 @parameter_name = '-ChangesPerHistory',
  7908.                 @parameter_value = '50'
  7909.         if (@retcode = 1 or @@ERROR <> 0)
  7910.             goto FAILURE
  7911.         exec @retcode = dbo.sp_add_agent_parameter 
  7912.                 @profile_id = @profile_id,
  7913.                 @parameter_name = '-BcpBatchSize',
  7914.                 @parameter_value = '100000'
  7915.         if (@retcode = 1 or @@ERROR <> 0)
  7916.             goto FAILURE
  7917.       end
  7918.  
  7919.     /* 
  7920.     ** Merge agent : (Synchronization Manager Profile)
  7921.     */
  7922.     else if (@profile_id = 9)
  7923.       begin
  7924.  
  7925.         exec @retcode = dbo.sp_add_agent_parameter 
  7926.                 @profile_id = @profile_id,
  7927.                 @parameter_name = '-PollingInterval',
  7928.                 @parameter_value = '60'
  7929.         if (@retcode = 1 or @@ERROR <> 0)
  7930.             goto FAILURE
  7931.  
  7932.         exec @retcode = dbo.sp_add_agent_parameter 
  7933.                 @profile_id = @profile_id,
  7934.                 @parameter_name = '-ValidateInterval',
  7935.                 @parameter_value = '60'
  7936.         if (@retcode = 1 or @@ERROR <> 0)
  7937.             goto FAILURE
  7938.  
  7939.         exec @retcode = dbo.sp_add_agent_parameter 
  7940.                 @profile_id = @profile_id,
  7941.                 @parameter_name = '-LoginTimeout',
  7942.                 @parameter_value = '15'
  7943.         if (@retcode = 1 or @@ERROR <> 0)
  7944.             goto FAILURE
  7945.  
  7946.         exec @retcode = dbo.sp_add_agent_parameter 
  7947.                 @profile_id = @profile_id,
  7948.                 @parameter_name = '-QueryTimeout',
  7949.                 @parameter_value = '300'
  7950.         if (@retcode = 1 or @@ERROR <> 0)
  7951.             goto FAILURE
  7952.  
  7953.         exec @retcode = dbo.sp_add_agent_parameter 
  7954.                 @profile_id = @profile_id,
  7955.                 @parameter_name = '-UploadGenerationsPerBatch',
  7956.                 @parameter_value = '100'
  7957.         if (@retcode = 1 or @@ERROR <> 0)
  7958.             goto FAILURE
  7959.  
  7960.         exec @retcode = dbo.sp_add_agent_parameter 
  7961.                 @profile_id = @profile_id,
  7962.                 @parameter_name = '-DownloadGenerationsPerBatch',
  7963.                 @parameter_value = '100'
  7964.         if (@retcode = 1 or @@ERROR <> 0)
  7965.             goto FAILURE
  7966.  
  7967.         exec @retcode = dbo.sp_add_agent_parameter 
  7968.                 @profile_id = @profile_id,
  7969.                 @parameter_name = '-UploadReadChangesPerBatch',
  7970.                 @parameter_value = '100'
  7971.         if (@retcode = 1 or @@ERROR <> 0)
  7972.             goto FAILURE
  7973.  
  7974.         exec @retcode = dbo.sp_add_agent_parameter 
  7975.                 @profile_id = @profile_id,
  7976.                 @parameter_name = '-DownloadReadChangesPerBatch',
  7977.                 @parameter_value = '100'
  7978.         if (@retcode = 1 or @@ERROR <> 0)
  7979.             goto FAILURE
  7980.  
  7981.         exec @retcode = dbo.sp_add_agent_parameter 
  7982.                 @profile_id = @profile_id,
  7983.                 @parameter_name = '-UploadWriteChangesPerBatch',
  7984.                 @parameter_value = '100'
  7985.         if (@retcode = 1 or @@ERROR <> 0)
  7986.             goto FAILURE
  7987.  
  7988.         exec @retcode = dbo.sp_add_agent_parameter 
  7989.                 @profile_id = @profile_id,
  7990.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  7991.                 @parameter_value = '100'
  7992.         if (@retcode = 1 or @@ERROR <> 0)
  7993.             goto FAILURE
  7994.  
  7995.         exec @retcode = dbo.sp_add_agent_parameter 
  7996.                 @profile_id = @profile_id,
  7997.                 @parameter_name = '-Validate',
  7998.                 @parameter_value = '0'
  7999.         if (@retcode = 1 or @@ERROR <> 0)
  8000.             goto FAILURE
  8001.         
  8002.         exec @retcode = dbo.sp_add_agent_parameter 
  8003.                 @profile_id = @profile_id,
  8004.                 @parameter_name = '-HistoryVerboseLevel',
  8005.                 @parameter_value = '1'
  8006.         if (@retcode = 1 or @@ERROR <> 0)
  8007.             goto FAILURE
  8008.  
  8009.         exec @retcode = dbo.sp_add_agent_parameter 
  8010.                 @profile_id = @profile_id,
  8011.                 @parameter_name = '-ChangesPerHistory',
  8012.                 @parameter_value = '50'
  8013.         if (@retcode = 1 or @@ERROR <> 0)
  8014.             goto FAILURE
  8015.         exec @retcode = dbo.sp_add_agent_parameter 
  8016.                 @profile_id = @profile_id,
  8017.                 @parameter_name = '-BcpBatchSize',
  8018.                 @parameter_value = '1000'
  8019.         if (@retcode = 1 or @@ERROR <> 0)
  8020.             goto FAILURE
  8021.       end
  8022.  
  8023.     /* 
  8024.     ** Distribution agent (Synchronization Manager Profile)
  8025.     */
  8026.     else if (@profile_id = 10)
  8027.       begin
  8028.  
  8029.         exec @retcode = dbo.sp_add_agent_parameter 
  8030.                 @profile_id = @profile_id,
  8031.                 @parameter_name = '-BcpBatchSize',
  8032.                 @parameter_value = '1000'
  8033.         if (@retcode = 1 or @@ERROR <> 0)
  8034.             goto FAILURE
  8035.  
  8036.         exec @retcode = dbo.sp_add_agent_parameter 
  8037.                 @profile_id = @profile_id,
  8038.                 @parameter_name = '-CommitBatchSize',
  8039.                 @parameter_value = '100'
  8040.         if (@retcode = 1 or @@ERROR <> 0)
  8041.             goto FAILURE
  8042.  
  8043.         exec @retcode = dbo.sp_add_agent_parameter 
  8044.                 @profile_id = @profile_id,
  8045.                 @parameter_name = '-CommitBatchThreshold',
  8046.                 @parameter_value = '1000'
  8047.         if (@retcode = 1 or @@ERROR <> 0)
  8048.             goto FAILURE
  8049.  
  8050.         exec @retcode = dbo.sp_add_agent_parameter 
  8051.                 @profile_id = @profile_id,
  8052.                 @parameter_name = '-HistoryVerboseLevel',
  8053.                 @parameter_value = '1'
  8054.         if (@retcode = 1 or @@ERROR <> 0)
  8055.             goto FAILURE
  8056.  
  8057.         exec @retcode = dbo.sp_add_agent_parameter 
  8058.                 @profile_id = @profile_id,
  8059.                 @parameter_name = '-LoginTimeout',
  8060.                 @parameter_value = '15'
  8061.         if (@retcode = 1 or @@ERROR <> 0)
  8062.             goto FAILURE
  8063.  
  8064.         exec @retcode = dbo.sp_add_agent_parameter 
  8065.                 @profile_id = @profile_id,
  8066.                 @parameter_name = '-MaxBcpThreads',
  8067.                 @parameter_value = '1'
  8068.         if (@retcode = 1 or @@ERROR <> 0)
  8069.             goto FAILURE
  8070.  
  8071.         exec @retcode = dbo.sp_add_agent_parameter 
  8072.                 @profile_id = @profile_id,
  8073.                 @parameter_name = '-MaxDeliveredTransactions',
  8074.                 @parameter_value = '0'
  8075.         if (@retcode = 1 or @@ERROR <> 0)
  8076.             goto FAILURE
  8077.  
  8078.         exec @retcode = dbo.sp_add_agent_parameter 
  8079.                 @profile_id = @profile_id,
  8080.                 @parameter_name = '-PollingInterval',
  8081.                 @parameter_value = '10'
  8082.         if (@retcode = 1 or @@ERROR <> 0)
  8083.             goto FAILURE
  8084.  
  8085.         exec @retcode = dbo.sp_add_agent_parameter 
  8086.                 @profile_id = @profile_id,
  8087.                 @parameter_name = '-QueryTimeout',
  8088.                 @parameter_value = '300'        -- 5 minutes 
  8089.         if (@retcode = 1 or @@ERROR <> 0)
  8090.             goto FAILURE
  8091.  
  8092.         exec @retcode = dbo.sp_add_agent_parameter 
  8093.                 @profile_id = @profile_id,
  8094.                 @parameter_name = '-SkipFailureLevel',
  8095.                 @parameter_value = '1'
  8096.         if (@retcode = 1 or @@ERROR <> 0)
  8097.             goto FAILURE
  8098.  
  8099.         exec @retcode = dbo.sp_add_agent_parameter 
  8100.                 @profile_id = @profile_id,
  8101.                 @parameter_name = '-TransactionsPerHistory',
  8102.                 @parameter_value = '100'
  8103.         if (@retcode = 1 or @@ERROR <> 0)
  8104.             goto FAILURE
  8105.  
  8106.       end
  8107.     
  8108.     return 0
  8109. FAILURE:
  8110.     return 1
  8111. go
  8112.  
  8113. dump tran msdb with no_log
  8114. GO
  8115.  
  8116. raiserror('Creating procedure sp_MSvalidate_agent_parameter', 0,1)
  8117. go
  8118. create procedure sp_MSvalidate_agent_parameter (
  8119.     @profile_id      int,
  8120.     @parameter_name  sysname,
  8121.     @parameter_value nvarchar(255)
  8122. )
  8123. as
  8124.     declare @agent_type  int
  8125.     declare @original_parameter_name sysname
  8126.     declare @numeric_value int
  8127.  
  8128.     -- Make sure parameters are non-null
  8129.     if @profile_id is null
  8130.     BEGIN
  8131.         RAISERROR (14043, 16, -1, '@profile_id')
  8132.         RETURN (1)
  8133.     END
  8134.  
  8135.     if @parameter_name is null 
  8136.     BEGIN
  8137.         RAISERROR (14043, 16, -1, '@parameter_name')
  8138.         RETURN (1)
  8139.     END
  8140.  
  8141.     IF @parameter_value is null
  8142.     BEGIN
  8143.         RAISERROR (14043, 16, -1, '@parameter_value')
  8144.         RETURN (1)
  8145.     END
  8146.  
  8147.     select @original_parameter_name = @parameter_name
  8148.     
  8149.     select @agent_type = agent_type
  8150.     from msdb..MSagent_profiles 
  8151.     where profile_id = @profile_id
  8152.  
  8153.     -- Parameter name validation
  8154.     if (substring(@parameter_name, 1, 1) <> '/' and 
  8155.         substring(@parameter_name, 1, 1) <> '-')
  8156.     begin
  8157.         return 1
  8158.     end
  8159.  
  8160.     select @parameter_name = lower(substring(@parameter_name, 2, len(@parameter_name) - 1))
  8161.  
  8162.     -- Snapshot agent - agent_type = 1
  8163.     if (@agent_type = 1)
  8164.     begin
  8165.         if not @parameter_name in ( 
  8166.             N'bcpbatchsize',   
  8167.             N'historyverboselevel',
  8168.             N'logintimeout',
  8169.             N'maxbcpthreads',
  8170.             N'querytimeout'
  8171.             )    
  8172.         begin
  8173.             raiserror(21111, 16, -1, @original_parameter_name)
  8174.             return 1
  8175.         end
  8176.     end
  8177.     -- Logreader - agent_type = 2
  8178.     else if (@agent_type =2)  
  8179.     begin
  8180.         if not lower(@parameter_name) in ( 
  8181.             N'historyverboselevel',
  8182.             N'logintimeout',
  8183.             N'pollinginterval',
  8184.             N'querytimeout',
  8185.             N'readbatchsize',
  8186.             N'readbatchthreshold'
  8187.             )    
  8188.         begin
  8189.             raiserror(21112, 16, -1, @original_parameter_name)
  8190.             return 1
  8191.         end
  8192.     end
  8193.     -- Distribution agent - agent_type = 3
  8194.     else if (@agent_type = 3)
  8195.     begin
  8196.         if not @parameter_name in ( 
  8197.             N'bcpbatchsize',   
  8198.             N'commitbatchsize',   
  8199.             N'commitbatchthreshold',   
  8200.             N'historyverboselevel',
  8201.             N'logintimeout',
  8202.             N'maxbcpthreads',
  8203.             N'maxdeliveredtransactions',
  8204.             N'pollinginterval',
  8205.             N'querytimeout',
  8206.             N'skipfailurelevel',
  8207.             N'transactionsperhistory'
  8208.             )    
  8209.         begin
  8210.             raiserror(21113, 16, -1, @original_parameter_name)
  8211.             return 1
  8212.         end
  8213.     end
  8214.     -- Merge agent - agent_type = 4
  8215.     else if (@agent_type = 4)
  8216.     begin 
  8217.         if not @parameter_name in ( 
  8218.             N'pollinginterval',   
  8219.             N'validateinterval',   
  8220.             N'logintimeout',   
  8221.             N'querytimeout',   
  8222.             N'uploadgenerationsperbatch',   
  8223.             N'downloadgenerationsperbatch',   
  8224.             N'uploadreadchangesperbatch',   
  8225.             N'downloadreadchangesperbatch',   
  8226.             N'uploadwritechangesperbatch',
  8227.             N'downloadwritechangesperbatch',
  8228.             N'validate',   
  8229.             N'fastrowcount',   
  8230.             N'historyverboselevel',
  8231.             N'changesperhistory',
  8232.             N'bcpbatchsize'
  8233.             )    
  8234.         begin
  8235.             raiserror(21114, 16, -1, @original_parameter_name)
  8236.             return 1
  8237.         end
  8238.     end
  8239.     else if @agent_type is null
  8240.     begin
  8241.         raiserror (20066, 16, -1)   -- profile not defined
  8242.         return 1
  8243.     end
  8244.     else
  8245.     begin
  8246.         -- MSagent_parameters table corruption
  8247.         return 1
  8248.     end
  8249.  
  8250.     -- Parameter value validation
  8251.     if (@parameter_name = N'bcpbatchsize')
  8252.     begin
  8253.         select @numeric_value = convert(int, @parameter_value)
  8254.         if @@error <> 0 or @numeric_value < 1
  8255.         begin
  8256.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8257.             return 1     
  8258.         end
  8259.     end
  8260.     else if (@parameter_name = N'commitbatchsize')
  8261.     begin
  8262.         select @numeric_value = convert(int, @parameter_value)
  8263.         if @@error <> 0 or @numeric_value < 1
  8264.         begin
  8265.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8266.             return 1     
  8267.         end                        
  8268.     end
  8269.     else if (@parameter_name = N'commitbatchthreshold')
  8270.     begin
  8271.         select @numeric_value = convert(int, @parameter_value)
  8272.         if @@error <> 0 or @numeric_value < 1
  8273.         begin
  8274.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8275.             return 1     
  8276.         end                        
  8277.     end
  8278.     else if (@parameter_name = N'downloadgenerationsperbatch')
  8279.     begin
  8280.         select @numeric_value = convert(int, @parameter_value)
  8281.         if @@error <> 0 or @numeric_value < 1
  8282.         begin
  8283.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8284.             return 1     
  8285.         end                        
  8286.     end
  8287.     else if (@parameter_name = N'downloadreadchangesperbatch')
  8288.     begin
  8289.         select @numeric_value = convert(int, @parameter_value)
  8290.         if @@error <> 0 or @numeric_value < 1
  8291.         begin
  8292.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8293.             return 1     
  8294.         end                        
  8295.     end
  8296.     else if (@parameter_name = N'downloadwritechangesperbatch')
  8297.     begin
  8298.         select @numeric_value = convert(int, @parameter_value)
  8299.         if @@error <> 0 or @numeric_value < 1
  8300.         begin
  8301.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8302.             return 1     
  8303.         end                        
  8304.     end
  8305.     else if (@parameter_name = N'fastrowcount')
  8306.     begin
  8307.         select @numeric_value = convert(int, @parameter_value)
  8308.         if @@error <> 0 or not (@numeric_value in (1,2,3))
  8309.         begin
  8310.             raiserror(21116, 16, -1, @parameter_value, @original_parameter_name)
  8311.             return 1     
  8312.         end                        
  8313.     end
  8314.     else if (@parameter_name = N'historyverboselevel')
  8315.     begin
  8316.         select @numeric_value = convert(int, @parameter_value)
  8317.         if @@error <> 0 or not (@numeric_value in (0,1,2,3))
  8318.         begin
  8319.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  8320.             return 1     
  8321.         end                        
  8322.     end
  8323.     else if (@parameter_name = N'logintimeout')
  8324.     begin
  8325.         select @numeric_value = convert(int, @parameter_value)
  8326.         if @@error <> 0 or @numeric_value < 1
  8327.         begin
  8328.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8329.             return 1     
  8330.         end                        
  8331.     end
  8332.     else if (@parameter_name = N'maxbcpthreads')
  8333.     begin
  8334.         select @numeric_value = convert(int, @parameter_value)
  8335.         if @@error <> 0 or @numeric_value < 1
  8336.         begin
  8337.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8338.             return 1     
  8339.         end                        
  8340.     end
  8341.     else if (@parameter_name = N'maxdeliveredtransactions')
  8342.     begin
  8343.         select @numeric_value = convert(int, @parameter_value)
  8344.         if @@error <> 0 or @numeric_value < 0
  8345.         begin
  8346.             raiserror(21119, 16, -1, @parameter_value, @original_parameter_name)
  8347.             return 1     
  8348.         end                        
  8349.     end
  8350.     else if (@parameter_name = N'pollinginterval')
  8351.     begin
  8352.         select @numeric_value = convert(int, @parameter_value)
  8353.         if @@error <> 0 or @numeric_value < 1
  8354.         begin
  8355.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8356.             return 1     
  8357.         end                        
  8358.     end
  8359.     else if (@parameter_name = N'querytimeout')
  8360.     begin
  8361.         select @numeric_value = convert(int, @parameter_value)
  8362.         if @@error <> 0 or @numeric_value < 1
  8363.         begin
  8364.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8365.             return 1     
  8366.         end                        
  8367.     end
  8368.     else if (@parameter_name = N'readbatchsize')
  8369.     begin
  8370.         select @numeric_value = convert(int, @parameter_value)
  8371.         if @@error <> 0 or @numeric_value < 1
  8372.         begin
  8373.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8374.             return 1     
  8375.         end                        
  8376.     end
  8377.     else if (@parameter_name = N'readbatchthreshold')
  8378.     begin
  8379.         select @numeric_value = convert(int, @parameter_value)
  8380.         if @@error <> 0 or @numeric_value < 1
  8381.         begin
  8382.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8383.             return 1     
  8384.         end                        
  8385.     end
  8386.     else if (@parameter_name = N'skipfailurelevel')
  8387.     begin
  8388.         select @numeric_value = convert(int, @parameter_value)
  8389.         if @@error <> 0 or @numeric_value < 0 
  8390.         begin
  8391.             raiserror(21119, 16, -1, @parameter_value, @original_parameter_name)
  8392.             return 1     
  8393.         end                        
  8394.     end
  8395.     else if (@parameter_name = N'transactionsperhistory')
  8396.     begin
  8397.         select @numeric_value = convert(int, @parameter_value)
  8398.         if @@error <> 0 or @numeric_value not between 0 and 10000
  8399.         begin
  8400.             raiserror(211118, 16, -1, @parameter_value, @original_parameter_name)
  8401.             return 1     
  8402.         end                        
  8403.     end
  8404.     else if (@parameter_name = N'uploadgenerationsperbatch')
  8405.     begin
  8406.         select @numeric_value = convert(int, @parameter_value)
  8407.         if @@error <> 0 or @numeric_value < 1
  8408.         begin
  8409.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8410.             return 1     
  8411.         end                        
  8412.     end
  8413.     else if (@parameter_name = N'uploadreadchangesperbatch')
  8414.     begin
  8415.         select @numeric_value = convert(int, @parameter_value)
  8416.         if @@error <> 0 or @numeric_value < 1
  8417.         begin
  8418.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8419.             return 1     
  8420.         end                        
  8421.     end
  8422.     else if (@parameter_name = N'uploadwritechangesperbatch')
  8423.     begin
  8424.         select @numeric_value = convert(int, @parameter_value)
  8425.         if @@error <> 0 or @numeric_value < 1
  8426.         begin
  8427.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8428.             return 1     
  8429.         end                        
  8430.     end
  8431.     else if (@parameter_name = N'validate')
  8432.     begin
  8433.         select @numeric_value = convert(int, @parameter_value)
  8434.         if @@error <> 0 or not (@numeric_value in (0,1,2))
  8435.         begin
  8436.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  8437.             return 1     
  8438.         end                        
  8439.     end
  8440.     else if (@parameter_name = N'validateinterval')
  8441.     begin
  8442.         select @numeric_value = convert(int, @parameter_value)
  8443.         if @@error <> 0 or @numeric_value < 1
  8444.         begin
  8445.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  8446.             return 1     
  8447.         end                        
  8448.     end
  8449.  
  8450.     return 0
  8451. go 
  8452.  
  8453. EXEC dbo.sp_MS_marksystemobject 'sp_MSvalidate_agent_parameter'
  8454.  
  8455.  
  8456. raiserror('Creating procedure sp_add_agent_parameter', 0,1)
  8457. go
  8458.  
  8459. -- Add a row into the "MSagent_parameters" table
  8460. create procedure sp_add_agent_parameter (
  8461.     @profile_id int,
  8462.     @parameter_name         sysname,
  8463.     @parameter_value        nvarchar(255)
  8464. )
  8465. as
  8466.     declare @slash_parameter_name sysname
  8467.     declare @dash_parameter_name sysname
  8468.     declare @retcode int
  8469.     
  8470.     set nocount on
  8471.  
  8472.     select @parameter_name = RTRIM(@parameter_name)
  8473.  
  8474.  
  8475.     -- Call proc to validate parameter value
  8476.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  8477.                         @profile_id = @profile_id, 
  8478.                         @parameter_name = @parameter_name,
  8479.                         @parameter_value = @parameter_value
  8480.     if @retcode <> 0
  8481.         RETURN(1)
  8482.  
  8483.  
  8484.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/'))
  8485.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-'))
  8486.      
  8487.  
  8488.     /* A parameter may be defined only once per profile */
  8489.     if exists ( select * from msdb..MSagent_parameters 
  8490.             where profile_id = @profile_id
  8491.             and (@slash_parameter_name = lower(parameter_name) 
  8492.             or @dash_parameter_name = lower(parameter_name)))
  8493.     BEGIN
  8494.         RAISERROR (20067, 16, -1, @parameter_name)  -- The parameter name ''%s'' already exists for the specified profile
  8495.         RETURN (1)
  8496.     END
  8497.  
  8498.     insert into msdb..MSagent_parameters
  8499.     values (@profile_id, @parameter_name, @parameter_value) ;
  8500.  
  8501.     if @@error <> 0
  8502.         return(1)
  8503. GO
  8504.  
  8505. raiserror('Creating procedure sp_change_agent_parameter', 0,1)
  8506. go
  8507. -- Change one parameter of a profile from the MSagent_parameters table
  8508. create procedure sp_change_agent_parameter(
  8509.     @profile_id int,
  8510.     @parameter_name         sysname,
  8511.     @parameter_value        nvarchar(255)
  8512. )AS
  8513.     declare @slash_parameter_name sysname
  8514.     declare @dash_parameter_name sysname
  8515.     declare @retcode int
  8516.  
  8517.     select @parameter_name = RTRIM(@parameter_name)
  8518.  
  8519.     -- Call proc to validate parameter value
  8520.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  8521.                         @profile_id = @profile_id, 
  8522.                         @parameter_name = @parameter_name,
  8523.                         @parameter_value = @parameter_value
  8524.     if @retcode <> 0
  8525.         RETURN(1)
  8526.  
  8527.  
  8528.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/'))
  8529.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-'))
  8530.    
  8531.     IF exists ( select * from msdb..MSagent_parameters 
  8532.             where profile_id = @profile_id and 
  8533.                 (@slash_parameter_name = lower(parameter_name) or
  8534.                  @dash_parameter_name = lower(parameter_name)))
  8535.     BEGIN
  8536.         update msdb..MSagent_parameters set value = @parameter_value where 
  8537.             profile_id = @profile_id and 
  8538.             (@slash_parameter_name = lower(parameter_name) or
  8539.             @dash_parameter_name = lower(parameter_name)) 
  8540.  
  8541.         if @@error <> 0
  8542.             return(1)
  8543.     END
  8544. GO
  8545.  
  8546. raiserror('Creating procedure sp_change_agent_profile', 0,1)
  8547. go
  8548. -- Change one parameter of a profile from the MSagent_profiles table
  8549. create procedure sp_change_agent_profile(
  8550.     @profile_id         int,
  8551.     @property           sysname,
  8552.     @value              nvarchar(3000)
  8553. )AS
  8554.  
  8555.     IF lower(@property) not in ('description')
  8556.     BEGIN
  8557.         RAISERROR('the @property parameter must be ''description''. ', 16, -1)
  8558.         RETURN (1)
  8559.     END
  8560.  
  8561.     IF exists ( select * from msdb..MSagent_profiles where profile_id = @profile_id)
  8562.     BEGIN
  8563.         update msdb..MSagent_profiles set description = @value where profile_id = @profile_id
  8564.         if @@error <> 0
  8565.             return(1)
  8566.     END
  8567. GO
  8568.  
  8569.  
  8570. raiserror('Creating procedure sp_help_agent_parameter', 0,1)
  8571. go
  8572. -- View all the parameters of a profile from the MSagent_parameters table
  8573. create procedure sp_help_agent_parameter(
  8574.     @profile_id     int = -1
  8575. )
  8576. as
  8577.     set nocount on
  8578.     
  8579.     if @profile_id = -1
  8580.     begin   
  8581.         select profile_id, parameter_name, value
  8582.         from msdb..MSagent_parameters
  8583.         order by profile_id, parameter_name
  8584.     end
  8585.     else
  8586.     begin
  8587.         select profile_id, parameter_name, value
  8588.         from msdb..MSagent_parameters
  8589.         where profile_id = @profile_id
  8590.         order by profile_id, parameter_name
  8591.     end
  8592. GO
  8593.  
  8594. raiserror('Creating procedure sp_MShelp_distdb', 0,1)
  8595. go
  8596.  
  8597. -- Called by the distribution agent to find the dist. db of a dist. publisher
  8598. create procedure sp_MShelp_distdb (     
  8599.     @publisher_name     sysname
  8600. )
  8601. as
  8602. if exists (select name from msdb..sysobjects where name = 'MSdistpublishers')
  8603. begin
  8604.     select distribution_db from msdb..MSdistpublishers
  8605.     where UPPER(name) = UPPER(@publisher_name)
  8606. end
  8607. else 
  8608.     select NULL where 1=2   /* We still want an empty rowset for some agents */
  8609. GO
  8610.  
  8611. dump tran master with no_log
  8612. GO
  8613.  
  8614. raiserror('Creating procedure sp_MSupdate_replication_status', 0,1)
  8615. go
  8616. create procedure sp_MSupdate_replication_status
  8617. @publisher sysname,
  8618. @publisher_db sysname,
  8619. @publication sysname,
  8620. @publication_type int = 0,      -- 0 Transactional/Snapshot 1 Merge
  8621. @agent_type int,
  8622. @agent_name nvarchar(100),
  8623. @status int
  8624. as
  8625.  
  8626.     declare @deleted int
  8627.     declare @refresh int
  8628.     declare @getstatus int
  8629.     declare @dummy int
  8630.  
  8631.  
  8632.     set @deleted = -1
  8633.     set @refresh = -2            -- Status used to only update the timestamp column
  8634.     set @getstatus = -3            -- Get status of agent for dummy distribution row
  8635.  
  8636.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  8637.         return 0
  8638.  
  8639.     if @status = @getstatus
  8640.     begin
  8641.         select @status = isnull(status, 0) from tempdb.dbo.MSreplication_agent_status where
  8642.             UPPER(publisher) = UPPER(@publisher) and
  8643.             publisher_db = @publisher_db and
  8644.             publication = 'ALL' and
  8645.             agent_type = @agent_type
  8646.     end            
  8647.  
  8648.     -- Update timestamp column via dummy update
  8649.     if @status = @refresh
  8650.     begin
  8651.         -- Dummy update to force timestamps to be updated.
  8652.         update tempdb.dbo.MSreplication_agent_status set status = status where
  8653.             UPPER(publisher) = UPPER(@publisher) and
  8654.             publisher_db = @publisher_db and
  8655.             publication like @publication -- Must use like as publication may be "%"
  8656.         return (0)
  8657.     end
  8658.  
  8659.     -- Remove row if @deleted
  8660.     if @status = @deleted
  8661.     begin
  8662.         if @@trancount > 0
  8663.         begin
  8664.             if exists (select * from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where 1 = 1)
  8665.             begin
  8666.                 select @dummy = 1
  8667.             end
  8668.         end
  8669.  
  8670.         if @agent_name = '%'
  8671.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  8672.                 UPPER(publisher) = UPPER(@publisher) and
  8673.                 publisher_db = @publisher_db and
  8674.                 publication = @publication and
  8675.                 agent_type = @agent_type 
  8676.         else if @agent_name = NULL    -- Special case when all agents of the type are dropped
  8677.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  8678.                 UPPER(publisher) = UPPER(@publisher) and
  8679.                 publisher_db = @publisher_db and
  8680.                 publication = @publication and
  8681.                 agent_type = @agent_type
  8682.         else
  8683.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  8684.                 UPPER(publisher) = UPPER(@publisher) and
  8685.                 publisher_db = @publisher_db and
  8686.                 publication = @publication and
  8687.                 agent_type = @agent_type and
  8688.                 agent_name = @agent_name
  8689.  
  8690.         -- Dummy update to force timestamps to be updated.  This will signal a row has been
  8691.         -- removed.
  8692.         update tempdb.dbo.MSreplication_agent_status set status = status where
  8693.             UPPER(publisher) = UPPER(@publisher) and
  8694.             publisher_db = @publisher_db and
  8695.             publication = @publication
  8696.         return (0)
  8697.     end     
  8698.  
  8699.     -- If misc. replication job then the status needs to be mapped.
  8700.     if @agent_type = 5
  8701.     begin
  8702.         set @status = 
  8703.         case isnull(@status,5)    -- mapped to never run
  8704.             when 0 then 6   -- Fail mapping
  8705.             when 1 then 2   -- Success mapping
  8706.             when 2 then 5   -- Retry mapping
  8707.             when 3 then 2   -- Shutdown mapping
  8708.             when 4 then 3   -- Inprogress mapping
  8709.             when 5 then 0   -- Unknown is mapped to never run
  8710.         end
  8711.     end
  8712.  
  8713.     if not exists (select * from tempdb.dbo.MSreplication_agent_status with (NOLOCK) where 
  8714.         UPPER(publisher) = UPPER(@publisher) and
  8715.         publisher_db = @publisher_db and
  8716.         publication = @publication and
  8717.         agent_type = @agent_type and
  8718.         agent_name = @agent_name)
  8719.         begin
  8720.             if @@trancount > 0
  8721.             begin
  8722.                 if exists (select * from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where 1 = 1)
  8723.                 begin
  8724.                     select @dummy = 1
  8725.                 end
  8726.             end
  8727.  
  8728.             insert into tempdb.dbo.MSreplication_agent_status 
  8729.                 (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name) values
  8730.                 (@publisher, @publisher_db, @publication, @publication_type, @agent_type, @status, @agent_name)
  8731.             -- Dummy update to force timestamps to be updated.  This will signal a row has been
  8732.             -- added.
  8733.             update tempdb.dbo.MSreplication_agent_status set status = status where
  8734.                 UPPER(publisher) = UPPER(@publisher) and
  8735.                 publisher_db = @publisher_db and
  8736.                 publication = @publication
  8737.         end
  8738.     else
  8739.         begin
  8740.             update tempdb.dbo.MSreplication_agent_status set status = @status where
  8741.                 UPPER(publisher) = UPPER(@publisher) and
  8742.                 publisher_db = @publisher_db and
  8743.                 (publication = @publication or @publication = 'ALL')and
  8744.                 agent_type = @agent_type and
  8745.                 agent_name = @agent_name
  8746.         end                 
  8747.  
  8748.     return (0)
  8749. go
  8750.  
  8751. raiserror('Creating procedure sp_MSenum_misc_agents', 0,1)
  8752. go
  8753. create procedure sp_MSenum_misc_agents
  8754. as
  8755.     SET NOCOUNT ON
  8756.  
  8757.     declare @timestamp timestamp
  8758.  
  8759.     -- Not supported, but returned to be consistent with other sp_MSenum_replication_agent
  8760.     -- result set.
  8761.     set @timestamp = 0
  8762.  
  8763.     select j.name, 'agent_type' = c.name,
  8764.             'status' = case isnull(jh.run_status, 5) -- mapped to never run
  8765.                 when 0 then 6   -- Fail mapping
  8766.                 when 1 then 2   -- Success mapping
  8767.                 when 2 then 5   -- Retry mapping
  8768.                 when 3 then 2   -- Shutdown mapping
  8769.                 when 4 then 3   -- Inprogress mapping
  8770.                 when 5 then 0   -- Unknown is mapped to never run
  8771.             end,
  8772.             jh.message, 'start_time' = convert(nvarchar(8), jh.run_date) + N' ' +
  8773.                                        stuff(stuff(right(convert(nvarchar(7), jh.run_time + 1000000), 6), 5, 0, N':'), 3, 0, N':') + 
  8774.                                        + N'.000',
  8775.             jh.run_duration, 
  8776.             'job_id' = convert(binary(16), j.job_id), 'local_timestamp' = @timestamp from 
  8777.                 msdb..sysjobs j
  8778.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  8779.             j.job_id = jh.job_id and 
  8780.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  8781.                 jh2.job_id = j.job_id)
  8782.             INNER JOIN msdb..syscategories c ON
  8783.             j.category_id = c.category_id
  8784.             where j.category_id in (11, 12,16,17,18)
  8785.  
  8786.     order by j.job_id asc
  8787. go 
  8788.  
  8789. raiserror('Creating procedure sp_MSload_replication_status', 0,1)
  8790. go
  8791.  
  8792. create proc sp_MSload_replication_status
  8793. as
  8794.     set nocount on
  8795.  
  8796.     declare @db_name sysname
  8797.     declare @distbit int
  8798.     declare @retcode int
  8799.  
  8800.     select @distbit = 16
  8801.     select @retcode = 0
  8802.  
  8803.     -- Create the table if it does not exist
  8804.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  8805.     begin
  8806.         exec @retcode = dbo.sp_MScreate_replication_status_table
  8807.         if @@error <> 0 or @retcode <> 0
  8808.             return 1
  8809.     end
  8810.     else
  8811.         -- Flush out all current rows
  8812.         truncate table tempdb.dbo.MSreplication_agent_status
  8813.  
  8814.     -- Load misc. replication jobs status and map SQL Agent status to replication status
  8815.     insert into tempdb.dbo.MSreplication_agent_status 
  8816.         select '', '', '', -1, 5, 
  8817.             case isnull(jh.run_status,5) -- mapped to never run
  8818.                 when 0 then 6   -- Fail mapping
  8819.                 when 1 then 2   -- Success mapping
  8820.                 when 2 then 5   -- Retry mapping
  8821.                 when 3 then 2   -- Shutdown mapping
  8822.                 when 4 then 3   -- Inprogress mapping
  8823.                 when 5 then 0   -- Unknown is mapped to never run
  8824.             end,
  8825.             j.name, NULL from msdb..sysjobs j
  8826.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  8827.             j.job_id = jh.job_id and 
  8828.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  8829.                 jh2.job_id = j.job_id)
  8830.             where j.category_id in (11,12,16,17,18)
  8831.     if @@error <> 0
  8832.         return 1
  8833.  
  8834.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  8835.         select name from master.dbo.sysdatabases 
  8836.             where
  8837.             category & @distbit <> 0 and
  8838.             (isnull(databaseproperty(name, N'issuspect'), 0) = 0 and isnull(databaseproperty(name, N'isshutdown'), 0) = 0)
  8839.         for read only
  8840.  
  8841.     open hCdatabase
  8842.     fetch next from hCdatabase into @db_name
  8843.     while (@@fetch_status <> -1)
  8844.     begin
  8845.  
  8846.         insert into tempdb.dbo.MSreplication_agent_status 
  8847.             (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name)
  8848.             exec (@db_name + '.dbo.sp_MSenum_replication_status')
  8849.  
  8850.         fetch next from hCdatabase into @db_name
  8851.     end
  8852.         
  8853. DONE:
  8854.     close hCdatabase
  8855.     deallocate hCdatabase
  8856.  
  8857.  
  8858.     -- Add and initialize Perfmon SQL Replication Agents instances
  8859.     exec @retcode = dbo.sp_MSinit_replication_perfmon
  8860.     if @retcode <> 0 or @@error <> 0
  8861.         return 1
  8862.  
  8863. GO
  8864.  
  8865. raiserror('Creating procedure sp_MScreate_replication_status_table', 0,1)
  8866. go
  8867. create proc sp_MScreate_replication_status_table
  8868. as
  8869.     declare @retcode int
  8870.  
  8871.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  8872.     begin
  8873.         -- begin tran
  8874.         create table tempdb.dbo.MSreplication_agent_status (
  8875.             publisher sysname NOT NULL,
  8876.             publisher_db sysname NOT NULL,
  8877.             publication sysname NOT NULL,
  8878.             publication_type int NOT NULL,          -- 0 transactional/snapshot  1 Merge
  8879.             agent_type int NOT NULL,
  8880.             status int NOT NULL,
  8881.             agent_name nvarchar(100) NOT NULL,
  8882.             timestamp NOT NULL,
  8883.             CONSTRAINT pk_MSrepl PRIMARY KEY (timestamp, agent_type)
  8884.             )
  8885.         if @@error <> 0
  8886.             return 1
  8887.         
  8888.         create nonclustered index uncMSreplication_agent_status ON 
  8889.             tempdb.dbo.MSreplication_agent_status (status, publisher, publisher_db, publication, agent_type)
  8890.         
  8891.         if @@error <> 0
  8892.             return 1
  8893.     end
  8894.     return 0  -- If here, all is well and we're done.
  8895. go
  8896.  
  8897. raiserror('Creating procedure sp_MShelp_replication_status', 0,1)
  8898. go
  8899.  
  8900. create procedure sp_MShelp_replication_status
  8901. @publisher sysname = '%',
  8902. @publisher_db sysname = '%',
  8903. @publication sysname = '%',
  8904. @agent_type int = 0
  8905. as
  8906.     declare @retcode int
  8907.     declare @max_status int
  8908.     declare @max_timestamp timestamp
  8909.     declare @publication_type int
  8910.     declare @all_status int
  8911.     declare @all_timestamp timestamp
  8912.  
  8913.  
  8914.     -- If the temporary status table does not exist, create it and populate it.
  8915.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  8916.     begin                                           
  8917.         exec @retcode = dbo.sp_MSload_replication_status
  8918.         if @@error <> 0
  8919.             return 1
  8920.     end
  8921.  
  8922.  
  8923.     -- @agent_type is only supported with all wildcards
  8924.     if @agent_type <> 0 and (@publisher <> '%' or @publisher_db <> '%' or @publication <> '%')
  8925.         return 1
  8926.     
  8927.     if @publisher = '%'
  8928.     begin
  8929.         -- @agent_type = 0 returns status of all agents including the misc. agents.
  8930.         -- @agent_type = -1 returns status of all agents except misc. agents.
  8931.         if @agent_type = 0 or @agent_type = -1
  8932.         begin
  8933.             select @max_status = max(status) from tempdb.dbo.MSreplication_agent_status (NOLOCK)
  8934.                 where (@agent_type = -1 and agent_type <> 5) or
  8935.                       @agent_type = 0
  8936.             select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK)
  8937.                 where (@agent_type = -1 and agent_type <> 5) or
  8938.                       @agent_type = 0
  8939.         end
  8940.         else
  8941.         begin
  8942.             select @max_status = max(status) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8943.                 agent_type = @agent_type
  8944.             select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8945.                 agent_type = @agent_type
  8946.         end
  8947.     end
  8948.     else if @publication = '%'
  8949.     begin
  8950.         select @max_status = max(status) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8951.             UPPER(publisher) = UPPER(@publisher) 
  8952. --          publisher_db = @publisher_db 
  8953.         select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8954.             UPPER(publisher) = UPPER(@publisher) 
  8955. --          publisher_db = @publisher_db 
  8956.     end
  8957.     else if @publication <> '%'
  8958.     begin
  8959.         select top 1 @max_status = status from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8960.             UPPER(publisher) = UPPER(@publisher) and
  8961.             publisher_db = @publisher_db and
  8962.             publication = @publication
  8963.             order by status desc
  8964.         
  8965.         -- Get publication type from the Snapshot agent of the Publication. This used to determine
  8966.         -- if the Logreader status should be included.
  8967.         select @publication_type = publication_type from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8968.             UPPER(publisher) = UPPER(@publisher) and
  8969.             publisher_db = @publisher_db and
  8970.             publication = @publication and
  8971.             agent_type = 1    -- Snapshot agent
  8972.  
  8973.         select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8974.             UPPER(publisher) = UPPER(@publisher) and
  8975.             publisher_db = @publisher_db and
  8976.             publication = @publication 
  8977.  
  8978.         -- If publication_type = 0 then transactional and must include the Logreader
  8979.         -- with publication = 'ALL'
  8980.         if @publication_type = 0
  8981.         begin
  8982.             select @all_status = status, @all_timestamp = timestamp from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  8983.                 UPPER(publisher) = UPPER(@publisher) and
  8984.                 publisher_db = @publisher_db and
  8985.                 publication = 'ALL' and
  8986.                 agent_type = 2 --Logreader
  8987.  
  8988.             if @all_status > @max_status
  8989.                 set @max_status = @all_status
  8990.  
  8991.             if @all_timestamp > @max_timestamp
  8992.                 set @max_timestamp = @all_timestamp
  8993.         end
  8994.  
  8995.     end
  8996.  
  8997.     -- Return result set
  8998.     select 'status' = isnull(@max_status, 0), 'timestamp' = isnull (@max_timestamp, 0)
  8999.  
  9000.     return (0)
  9001. go
  9002.  
  9003. raiserror('Creating procedure sp_MSenum_replication_agents', 0,1)
  9004. go
  9005. create procedure sp_MSenum_replication_agents
  9006. @type int
  9007. as
  9008.     set nocount on
  9009.  
  9010.     declare @snapshot int
  9011.     declare @logreader int
  9012.     declare @distribution int
  9013.     declare @sproc sysname
  9014.     declare @db_name sysname
  9015.     declare @table_name sysname
  9016.     declare @cmd nvarchar(255)
  9017.     declare @distbit int
  9018.     declare @merge int
  9019.     declare @misc int
  9020.  
  9021.     select @distbit = 16
  9022.  
  9023.     select @snapshot = 1
  9024.     select @logreader = 2
  9025.     select @distribution = 3
  9026.     select @merge = 4
  9027.     select @misc = 5
  9028.  
  9029.     if @type = @misc
  9030.     begin
  9031.         exec dbo.sp_MSenum_misc_agents
  9032.         return 0
  9033.     end
  9034.  
  9035.     if @type = @snapshot
  9036.     begin
  9037.         select @sproc = 'sp_MSenum_snapshot'
  9038.         create table #snapshot (dbname sysname NOT NULL, name nvarchar(100) NOT NULL, 
  9039.             status int NOT NULL,
  9040.             publisher sysname NOT NULL, publisher_db sysname NOT NULL, 
  9041.             publication sysname NOT NULL,
  9042.             start_time nvarchar(24) NULL, time nvarchar(24) NULL, duration int NULL,
  9043.             comments nvarchar(255) NULL, delivered_transactions int NULL, 
  9044.             delivered_commands int NULL, delivery_rate float NULL, error_id INT NULL,
  9045.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  9046.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  9047.  
  9048.         create unique clustered index ucsnapshot ON #snapshot (job_id)
  9049.  
  9050.         select @table_name = '#snapshot'
  9051.     end
  9052.     else if @type = @logreader
  9053.     begin
  9054.         select @sproc = 'sp_MSenum_logreader'
  9055.         create table #logreader (dbname sysname NOT NULL, name nvarchar(100) NOT NULL,  
  9056.             status int NOT NULL,
  9057.             publisher sysname NOT NULL, publisher_db sysname NOT NULL,
  9058.             start_time nvarchar(24) NULL, time nvarchar(24) NULL, duration int NULL,
  9059.             comments nvarchar(255) NULL, delivery_time int NULL, 
  9060.             delivered_transactions int NULL, delivered_commands int NULL, 
  9061.             average_commands int NULL, delivery_rate int NULL, delivery_latency int NULL, 
  9062.             error_id INT NULL, job_id binary(16) NULL, local_job bit NULL,
  9063.             profile_id int NOT NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  9064.  
  9065.         create unique clustered index uclogreader ON #logreader (job_id)
  9066.  
  9067.         select @table_name = '#logreader'
  9068.     end
  9069.     else if @type = @distribution
  9070.     begin
  9071.         select @sproc = 'sp_MSenum_distribution'
  9072.         create table #distribution (dbname sysname NOT NULL, 
  9073.             name nvarchar(100) NOT NULL,  
  9074.             status int NOT NULL,
  9075.             publisher sysname NOT NULL, publisher_db sysname NOT NULL, 
  9076.             publication sysname NULL,
  9077.             subscriber sysname NULL, subscriber_db sysname NULL, subscription_type int NULL,
  9078.             start_time nvarchar(24) NULL, time nvarchar(24) NULL, duration int NULL,
  9079.             comments nvarchar(255) NULL, delivery_time int NULL, 
  9080.             delivered_transactions int NULL, delivered_commands int NULL, 
  9081.             average_commands int NULL, delivery_rate int NULL, 
  9082.             delivery_latency int NULL, error_id INT NULL,
  9083.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  9084.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  9085.  
  9086.         create unique clustered index ucdistribution ON #distribution (job_id)
  9087.  
  9088.         select @table_name = '#distribution'
  9089.     end
  9090.     else if @type = @merge
  9091.     begin
  9092.         select @sproc = 'sp_MSenum_merge'
  9093.         create table #merge (dbname sysname NOT NULL, name nvarchar(100) NOT NULL, 
  9094.             status int NOT NULL,
  9095.             publisher sysname NOT NULL, publisher_db sysname NOT NULL, publication sysname NULL,
  9096.             subscriber sysname NULL, subscriber_db sysname NULL, subscription_type int NULL,
  9097.             start_time nvarchar(24) NULL, time nvarchar(24) NULL, duration int NULL, 
  9098.             comments nvarchar(255) NULL, delivery_rate int NULL,
  9099.             publisher_insertcount int NULL, publisher_updatecount int NULL, publisher_deletecount int NULL,
  9100.             publisher_conficts int NULL, 
  9101.             subscriber_insertcount int NULL, subscriber_updatecount int NULL, subscriber_deletecount int NULL,
  9102.             subscriber_conficts int NULL, error_id int NULL, job_id binary(16) NULL,
  9103.             local_job bit NULL, profile_id int NOT NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  9104.  
  9105.         create unique clustered index ucmerge ON #merge (job_id)
  9106.  
  9107.  
  9108.         select @table_name = '#merge'
  9109.     end
  9110.  
  9111.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  9112.         select name from master.dbo.sysdatabases 
  9113.             where
  9114.             category & @distbit <> 0 and
  9115.             (isnull(databaseproperty(name, N'issuspect'), 0) = 0 and isnull(databaseproperty(name, N'isshutdown'), 0) = 0)
  9116.         for read only
  9117.  
  9118.     open hCdatabase
  9119.     fetch next from hCdatabase into @db_name
  9120.     while (@@fetch_status <> -1)
  9121.     begin
  9122.         select @cmd = 'insert into ' + @table_name +
  9123.             ' exec ' + @db_name + '.dbo.' + @sproc + ' @show_distdb = 1'
  9124.         exec (@cmd)
  9125.         fetch next from hCdatabase into @db_name
  9126.     end
  9127.     close hCdatabase
  9128.     deallocate hCdatabase
  9129.  
  9130.     select @cmd = 'select * from ' + @table_name + ' order by job_id asc'
  9131.     exec (@cmd)
  9132.  
  9133.     -- Drop the table
  9134.     select @cmd = 'drop table ' + @table_name
  9135.     exec (@cmd)
  9136.  
  9137. GO
  9138.  
  9139. raiserror('Creating procedure sp_replication_agent_checkup', 0,1)
  9140. go
  9141. create procedure sp_replication_agent_checkup
  9142. @heartbeat_interval int = 10        -- minutes
  9143. as
  9144.     declare @distribution_db sysname
  9145.     declare @retstatus int
  9146.     declare @proc nvarchar(255)
  9147.     declare @retcode int
  9148.  
  9149.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD for
  9150.         select name from msdb..MSdistributiondbs where 
  9151.         (isnull(databaseproperty(name, N'issuspect'), 0) = 0 and isnull(databaseproperty(name, N'isshutdown'), 0) = 0)    
  9152.     for read only
  9153.     open hCdistdbs
  9154.     fetch hCdistdbs into @distribution_db
  9155.     while @@fetch_status <> -1
  9156.     begin
  9157.         select @proc = @distribution_db + '.dbo.sp_MSagent_stethoscope'
  9158.         execute  @retcode = @proc @heartbeat_interval
  9159.         if @@error <> 0 or @retcode <> 0
  9160.         begin
  9161.             select @retstatus = 1
  9162.             goto UNDO
  9163.         end
  9164.         fetch hCdistdbs into @distribution_db
  9165.     end
  9166.  
  9167.     set @retstatus = 0
  9168.  
  9169. UNDO:
  9170.     close hCdistdbs
  9171.     deallocate hCdistdbs
  9172.     return (@retstatus)
  9173. go
  9174.  
  9175. raiserror('Creating procedure sp_MScreate_replication_checkup_agent', 0,1)
  9176. go
  9177. create procedure sp_MScreate_replication_checkup_agent
  9178. @heartbeat_interval int = 10    -- minutes
  9179. as
  9180.     declare @command nvarchar(100)
  9181.     declare @retcode int
  9182.     declare @interval int
  9183.     declare @job_name nvarchar(100)
  9184.     declare @description nvarchar(100)
  9185.     declare @category_name sysname
  9186.  
  9187.     select @command = 'sp_replication_agent_checkup @heartbeat_interval = ' +
  9188.         convert(nvarchar(10), @heartbeat_interval)      
  9189.         
  9190.     -- Create job if it already exists
  9191.     select @job_name = formatmessage(20533)
  9192.  
  9193.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @job_name and
  9194.         originating_server = '(local)')
  9195.     BEGIN
  9196.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @job_name
  9197.         IF @@ERROR <> 0 or @retcode <> 0
  9198.         BEGIN
  9199.             return (1)            
  9200.         END
  9201.     END
  9202.  
  9203.     -- Create new job
  9204.     set @interval = convert(int, @heartbeat_interval)
  9205.     set @description = formatmessage(20534)
  9206.  
  9207.     -- Get Checkup category name (assumes category_id = 16)
  9208.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 16
  9209.     EXECUTE @retcode = dbo.sp_MSadd_repl_job 
  9210.             @name = @job_name,
  9211.             @subsystem = 'TSQL', 
  9212.             @enabled = 1, 
  9213.             @command = @command,
  9214.             @description = @description,
  9215.             @freqtype = 4,
  9216.             @freqinterval = 1,
  9217.             @freqsubtype = 4,
  9218.             @freqsubinterval = @interval,
  9219.             @retryattempts = 0,
  9220.             @category_name = @category_name
  9221.     if @@ERROR <> 0 or @retcode <> 0
  9222.         return (1)
  9223. go
  9224.  
  9225. raiserror('Creating procedure sp_MSenum_replication_job', 0,1)
  9226. go
  9227. create procedure sp_MSenum_replication_job
  9228. @job_id uniqueidentifier
  9229. as
  9230.     SET NOCOUNT ON
  9231.     declare @message nvarchar(1024)
  9232.     declare @retcode int
  9233.     declare @runstatus int
  9234.     declare @date int
  9235.     declare @time int
  9236.  
  9237.     -- Get last message from SQL Agent History table
  9238.     create table #JobHistory (
  9239.         instance_id int NOT NULL, 
  9240.         job_id uniqueidentifier NOT NULL,
  9241.         job_name nvarchar(100) NOT NULL,
  9242.         step_id int NOT NULL,
  9243.         step_name nvarchar(100) NOT NULL, 
  9244.         sql_message_id int NOT NULL,
  9245.         sql_severity int NOT NULL,
  9246.         message nvarchar(1024) NULL,
  9247.         run_status int NOT NULL,
  9248.         run_date int NOT NULL,
  9249.         run_time int NOT NULL,
  9250.         run_duration int NOT NULL,
  9251.         operator_emailed sysname NULL,
  9252.         operator_netsent sysname NULL,
  9253.         operator_paged sysname NULL,
  9254.         retries_attempted int NOT NULL,
  9255.         server sysname NOT NULL
  9256.     )
  9257.     if @@error <> 0
  9258.         return 1
  9259.  
  9260.     -- Insert last history for step_id 1 (Agent running)
  9261.     set rowcount 1
  9262.     insert into #JobHistory exec msdb.dbo.sp_help_jobhistory @job_id = @job_id, @step_id = 1, 
  9263.         @mode = 'FULL'          
  9264.  
  9265.     -- Get the last history
  9266.     select @message = message, @runstatus = run_status, @date = run_date, @time = run_time
  9267.         from #JobHistory
  9268.  
  9269.     -- Reset rowcount
  9270.     set rowcount 0
  9271.  
  9272.     -- Map SQL Agent runstatus to Replication runstatus
  9273.     set @runstatus = 
  9274.     case @runstatus
  9275.         when 0 then 6   -- Fail mapping
  9276.         when 1 then 2   -- Success mapping
  9277.         when 2 then 5   -- Retry mapping
  9278.         when 3 then 2   -- Shutdown mapping
  9279.         when 4 then 3   -- Inprogress mapping
  9280.         when 5 then 0   -- Unknown is mapped to never run
  9281.     end
  9282.  
  9283.     -- Return status and message
  9284.     select 'runstatus' = isnull(@runstatus, 0), 'message' = @message,  'date' = @date, 
  9285.         'time' = @time
  9286.  
  9287.     drop table #JobHistory
  9288. go 
  9289.  
  9290. raiserror('Creating procedure sp_MSrepl_dbrole', 0,1)
  9291. go
  9292. CREATE PROCEDURE sp_MSrepl_dbrole
  9293.     @rolename       sysname,
  9294.     @loginname      sysname,
  9295.     @operation      nvarchar(10),
  9296.     @is_member      bit = 0 OUTPUT
  9297. AS
  9298.     -- This is an internal stored procedure. 
  9299.     -- If operation is 'add', it will add the login as a user to current database if necessary
  9300.     -- get the user name of the login and add it to the role
  9301.     -- If operation is 'drop', it will drop the user from the role.
  9302.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  9303.     declare @retcode int
  9304.     declare @username sysname
  9305.     declare @sid varbinary(85)
  9306.  
  9307.     select @sid = suser_sid(@loginname)
  9308.  
  9309.     if @operation = 'is_member'
  9310.     begin
  9311.         if exists (select * from sysusers r, sysusers u, sysmembers m where 
  9312.             u.sid = @sid and
  9313.             r.name = @rolename and
  9314.             m.groupuid = r.uid and
  9315.             m.memberuid = u.uid)
  9316.             select @is_member = 1
  9317.         else
  9318.             select @is_member = 0
  9319.         return(0)
  9320.     end
  9321.     
  9322.     -- Add the login to  db  role.
  9323.     if  is_srvrolemember('sysadmin',  @loginname) <> 1
  9324.     begin
  9325.         select @username = name from sysusers where sid = @sid
  9326.         if @operation = 'add'
  9327.         begin
  9328.             if @username is null
  9329.             begin
  9330.                 -- Add the server login to be a user with same name in the database
  9331.                 exec @retcode = dbo.sp_adduser  @loginname
  9332.                 if @retcode<>0 or @@error <> 0
  9333.                     return 1
  9334.                 select @username = @loginname
  9335.             end
  9336.  
  9337.             exec @retcode = dbo.sp_addrolemember @rolename, @username
  9338.             if @@error <> 0 OR @retcode <> 0
  9339.                 return 1
  9340.         end
  9341.         else if @operation = 'drop'
  9342.         begin
  9343.             if @username is not null
  9344.             begin
  9345.                 exec @retcode = dbo.sp_droprolemember @rolename, @username
  9346.                 if @@error <> 0 OR @retcode <> 0
  9347.                     return 1
  9348.             end
  9349.         end
  9350.     end     
  9351. go
  9352.  
  9353. raiserror('Creating procedure sp_changedistributor_password', 0,1)
  9354. go
  9355. CREATE PROCEDURE sp_changedistributor_password (
  9356.     @password sysname       
  9357.     ) AS
  9358.  
  9359.     declare @distributor sysname
  9360.     declare @distnetname sysname
  9361.     declare @retcode int
  9362.     declare @login sysname
  9363.  
  9364.     select @login = 'distributor_admin'
  9365.     SELECT @distributor = NULL
  9366.     SELECT @distributor = srvname, @distnetname = datasource 
  9367.         FROM master..sysservers
  9368.         WHERE  srvstatus & 8 <> 0
  9369.     
  9370.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  9371.         @rmtsrvname= @distributor,
  9372.         @useself = 'false',
  9373.         @locallogin = NULL,
  9374.         @rmtuser = @login,
  9375.         @rmtpassword = @password
  9376.     IF @@error <> 0 OR @retcode <> 0
  9377.     BEGIN
  9378.         RETURN (1)
  9379.     END
  9380.  
  9381.     if UPPER(@distnetname) = UPPER(@@servername)
  9382.     begin
  9383.             -- Change the password if the distributor is local
  9384.         EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  9385.         if @@error <> 0 or @retcode <> 0
  9386.             return(1)
  9387.     end
  9388.  
  9389.     return (0)
  9390. GO
  9391.  
  9392. raiserror('Creating procedure sp_oledbinfo', 0,1)
  9393. go
  9394. CREATE PROCEDURE sp_oledbinfo
  9395.     @server nvarchar(128),
  9396.     @infotype nvarchar(128) = NULL,
  9397.     @login nvarchar(128) = NULL,
  9398.     @password nvarchar(128) = NULL
  9399.     AS
  9400.  
  9401.     SET NOCOUNT ON
  9402.  
  9403.     DECLARE @distributor sysname
  9404.     DECLARE @distproc nvarchar (255)
  9405.     DECLARE @retcode int
  9406.  
  9407.     /*
  9408.     ** Get distribution server information for remote RPC
  9409.     ** subscription calls.
  9410.     */
  9411.  
  9412.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  9413.     IF @@error <> 0 OR @retcode <> 0
  9414.         BEGIN
  9415.         RAISERROR (14071, 16, -1)
  9416.             RETURN (1)
  9417.     END
  9418.  
  9419.     select @distproc = rtrim(@distributor) + '.master.dbo.sp_MSget_oledbinfo'
  9420.     exec @retcode = @distproc @server, @infotype, @login, @password
  9421.     IF @@error <> 0
  9422.         BEGIN
  9423.         RAISERROR (14071, 16, -1)
  9424.         RETURN (1)
  9425.     END
  9426.  
  9427. go
  9428.  
  9429. raiserror('Creating procedure sp_MSget_oledbinfo', 0,1)
  9430. go
  9431. CREATE PROCEDURE sp_MSget_oledbinfo
  9432.     @server nvarchar(128),  -- the name by which the oledb datasource is referred to.
  9433.     @infotype nvarchar(128) = NULL,
  9434.     @login nvarchar(128) = NULL,
  9435.     @password nvarchar(128) = NULL
  9436.     AS
  9437.  
  9438.     SET NOCOUNT ON
  9439.  
  9440.     DECLARE @distproc nvarchar (255)
  9441.     DECLARE @providername nvarchar(255)
  9442.     DECLARE @datasource nvarchar(255)
  9443.     DECLARE @location nvarchar(255)
  9444.     DECLARE @providerstring nvarchar(255)
  9445.     DECLARE @catalog nvarchar(255)
  9446.     DECLARE @retcode int
  9447.  
  9448.     select @providername = providername, @datasource = datasource, @location = location, @providerstring = providerstring, @catalog = catalog
  9449.         from master..sysservers where UPPER(srvname) = UPPER(@server)
  9450.  
  9451.     if (@@rowcount = 0)
  9452.     begin
  9453.         raiserror(15015, 16, -1, @server)
  9454.         return (1)
  9455.     end
  9456.  
  9457.     exec @retcode = master..xp_oledbinfo @providername, @datasource, @location, @providerstring, @catalog, @login, @password, @infotype
  9458.     IF @@error <> 0
  9459.         BEGIN
  9460.         RAISERROR (14071, 16, -1)
  9461.         RETURN (1)
  9462.     END
  9463.  
  9464. go
  9465.  
  9466. raiserror('Creating procedure sp_grant_publication_access', 0,1)
  9467. go
  9468. CREATE PROCEDURE sp_grant_publication_access (
  9469.     @publication sysname,
  9470.     @login sysname,
  9471.     @reserved nvarchar(10) = NULL
  9472.         ) AS
  9473.  
  9474.     -- This stored procedure can be called repeatedly.
  9475.     DECLARE @distribdb sysname
  9476.     DECLARE @distproc nvarchar (255)
  9477.     DECLARE @retcode int
  9478.     DECLARE @dist_rpcname sysname
  9479.     DECLARE @database sysname
  9480.     
  9481.     -- Security Check
  9482.     exec @retcode = dbo.sp_MSreplcheck_publish
  9483.     if @@ERROR <> 0 or @retcode <> 0
  9484.         return(1)
  9485.     
  9486.     select @database = db_name()
  9487.  
  9488.     -- Existance check of the publication will be done in sp_MSpublication_access
  9489.     -- Note, even if the login exists, it may or may not has access
  9490.     -- to the server (granted or denied). 
  9491.     if @reserved is null
  9492.     begin
  9493.         if not exists (select * from master..syslogins where 
  9494.             sid = suser_sid(@login) and
  9495.             hasaccess = 1)
  9496.         begin
  9497.             raiserror(15007, 16, -1, @login) 
  9498.             return (1)
  9499.         end
  9500.         
  9501.         -- Don't do the user check if sysadmin since sysadmin can enter
  9502.         -- the database as dbo.
  9503.         if is_srvrolemember('sysadmin') = 0 and
  9504.             not exists (select * from sysusers where
  9505.             (sid = suser_sid(@login) or name = N'guest') and
  9506.             hasdbaccess = 1) 
  9507.         begin
  9508.             declare @login_len int
  9509.             declare @db_len int
  9510.             select @login_len = datalength(@login)
  9511.             select @db_len = datalength(@database)
  9512.             raiserror(916, 16, -1, @login_len, @login, 
  9513.                 @db_len,@database) 
  9514.             return (1)            
  9515.         end
  9516.     end
  9517.  
  9518.     select @database = db_name()
  9519.  
  9520.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  9521.                                         @distribdb   = @distribdb   OUTPUT
  9522.     IF @@error <> 0
  9523.     BEGIN
  9524.          RAISERROR (14071, 16, -1)
  9525.          return (1)
  9526.     END
  9527.  
  9528.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  9529.     BEGIN
  9530.         RAISERROR (14071, 16, -1)
  9531.         return(1)
  9532.     END
  9533.  
  9534.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  9535.     
  9536.     declare @skip bit
  9537.  
  9538.     declare @login2 sysname
  9539.  
  9540.     if @reserved = 'init'
  9541.     begin
  9542.         -- Skip logins that are not at the distributor without
  9543.         -- raiseing error.
  9544.         set @skip = 1
  9545.         declare hC CURSOR LOCAL FAST_FORWARD for
  9546.             select loginname from master..syslogins where
  9547.                 (is_srvrolemember('sysadmin', loginname) = 1 or
  9548.                 sid = suser_sid()) 
  9549.         for read only
  9550.     end
  9551.     else
  9552.     begin
  9553.         set @skip = 0
  9554.         declare hC CURSOR LOCAL FAST_FORWARD for
  9555.                 select @login
  9556.             for read only
  9557.     end
  9558.  
  9559.     open hC
  9560.     fetch hC into @login2
  9561.  
  9562.     while (@@fetch_status <> -1)
  9563.     begin
  9564.         EXEC @retcode = @distproc
  9565.              @publisher = @@SERVERNAME,
  9566.              @publisher_db = @database,
  9567.              @publication = @publication,
  9568.              @login= @login2,
  9569.              @operation = 'add',
  9570.              @skip = @skip
  9571.         IF @@error <> 0 OR @retcode <> 0
  9572.             return (1)
  9573.  
  9574.         fetch hC into @login2
  9575.     end
  9576.     
  9577.     close hC
  9578.     deallocate hC
  9579. GO
  9580.  
  9581. raiserror('Creating procedure sp_revoke_publication_access', 0,1)
  9582. go
  9583. CREATE PROCEDURE sp_revoke_publication_access (
  9584.     @publication sysname,
  9585.     @login sysname
  9586.         ) AS
  9587.  
  9588.     -- This stored procedure can be called repeatedly.
  9589.     DECLARE @distribdb sysname
  9590.     DECLARE @distproc nvarchar (255)
  9591.     DECLARE @retcode int
  9592.     DECLARE @dist_rpcname sysname
  9593.     DECLARE @database sysname
  9594.  
  9595.     -- Security Check
  9596.     exec @retcode = dbo.sp_MSreplcheck_publish
  9597.     if @@ERROR <> 0 or @retcode <> 0
  9598.         return(1)
  9599.  
  9600.     -- Do check existense when dropping since the login might be dropped
  9601.     -- outside replication already.
  9602.  
  9603.     select @database = db_name()
  9604.  
  9605.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  9606.                                         @distribdb   = @distribdb   OUTPUT
  9607.     IF @@error <> 0
  9608.     BEGIN
  9609.          RAISERROR (14071, 16, -1)
  9610.          return (1)
  9611.     END
  9612.  
  9613.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  9614.     BEGIN
  9615.         RAISERROR (14071, 16, -1)
  9616.         return(1)
  9617.     END
  9618.  
  9619.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  9620.     EXEC @retcode = @distproc
  9621.          @publisher = @@SERVERNAME,
  9622.          @publisher_db = @database,
  9623.          @publication = @publication,
  9624.          @login= @login,
  9625.          @operation = 'drop'
  9626.  
  9627.     IF @@error <> 0 OR @retcode <> 0
  9628.         return (1)
  9629. GO
  9630.  
  9631. raiserror('Creating procedure sp_help_publication_access', 0,1)
  9632. go
  9633. CREATE PROCEDURE sp_help_publication_access (
  9634.     @publication sysname,
  9635.     @return_granted bit = 1,
  9636.     @login sysname = '%',
  9637.     @initial_list bit = 0
  9638.         ) AS
  9639.  
  9640.     set nocount on
  9641.     -- This stored procedure can be called repeatedly.
  9642.     DECLARE @distribdb sysname
  9643.     DECLARE @distproc nvarchar (255)
  9644.     DECLARE @retcode int
  9645.     DECLARE @dist_rpcname sysname
  9646.     DECLARE @database sysname
  9647.  
  9648.     exec @retcode = dbo.sp_MSreplcheck_publish
  9649.     if @@ERROR <> 0 or @retcode <> 0
  9650.         return(1)
  9651.  
  9652.     -- Do check existense when dropping since the login might be dropped
  9653.     -- outside replication already.
  9654.  
  9655.     select @database = db_name()
  9656.  
  9657.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  9658.                                         @distribdb   = @distribdb   OUTPUT
  9659.     IF @@error <> 0
  9660.     BEGIN
  9661.          RAISERROR (14071, 16, -1)
  9662.          return (1)
  9663.     END
  9664.  
  9665.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  9666.     BEGIN
  9667.         RAISERROR (14071, 16, -1)
  9668.         return(1)
  9669.     END
  9670.  
  9671.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  9672.  
  9673.     -- Get logins in the PAL if needed
  9674.     if @initial_list = 0
  9675.     begin
  9676.         create table #granted (login sysname NULL)    
  9677.         insert into #granted EXEC @retcode = @distproc
  9678.              @publisher = @@SERVERNAME,
  9679.              @publisher_db = @database,
  9680.              @publication = @publication,
  9681.              @operation = 'help',
  9682.              @login = @login
  9683.  
  9684.         IF @@error <> 0 OR @retcode <> 0
  9685.             return (1)
  9686.     end
  9687.  
  9688.     -- Get distributor valid logins if needed
  9689.     if @return_granted = 0 or @initial_list = 1
  9690.     begin
  9691.         create table #dist_logins(login sysname NULL)
  9692.         insert into #dist_logins EXEC @retcode = @distproc
  9693.              @publisher = @@SERVERNAME,
  9694.              @operation = 'get_logins'
  9695.     end
  9696.  
  9697.     if @initial_list = 1
  9698.     begin
  9699.         -- Get the initial list for the publication to be created
  9700.         -- by the current user
  9701.         -- It contains all the logins sysadmin group and the current user
  9702.         -- that have valid login at the distributor.
  9703.         select l.loginname, l.isntname, l.isntgroup
  9704.          from master..syslogins l, #dist_logins d where
  9705.             l.sid = suser_sid(d.login) and
  9706.             l.hasaccess = 1 and
  9707.             (is_srvrolemember('sysadmin',d.login) = 1 or (l.sid = suser_sid())) 
  9708.     end
  9709.     else if @return_granted = 0
  9710.     begin
  9711.         select l.loginname, l.isntname, l.isntgroup
  9712.          from master..syslogins l, #dist_logins d where
  9713.             l.sid = suser_sid(d.login) and
  9714.             l.hasaccess = 1 and 
  9715.             not exists (select * from #granted g where 
  9716.                 suser_sid(g.login) = l.sid) and
  9717.             exists (select * from sysusers u where
  9718.                 (u.sid = l.sid or u.name = 'guest') and
  9719.                 u.hasdbaccess = 1)
  9720.     end
  9721.     else if @return_granted = 1
  9722.     begin
  9723.         select l.loginname, l.isntname, l.isntgroup from master..syslogins l,
  9724.             #granted g where
  9725.             l.sid = suser_sid(g.login)
  9726.     end
  9727. GO
  9728.  
  9729. raiserror('Creating procedure sp_check_publication_access', 0,1)
  9730. go
  9731. CREATE PROCEDURE sp_check_publication_access
  9732. @publication sysname
  9733. AS
  9734.     declare @retcode int
  9735.     exec @retcode = dbo.sp_MSreplcheck_pull 
  9736.         @publication = @publication
  9737.     if @retcode <> 0 or @@error <> 0
  9738.         return (1)
  9739.     return 0
  9740. GO
  9741.  
  9742. exec dbo.sp_MS_marksystemobject sp_check_publication_access
  9743. go
  9744.  
  9745. raiserror('Creating procedure sp_MSinit_replication_perfmon', 0,1)
  9746. go
  9747.  
  9748. create proc sp_MSinit_replication_perfmon
  9749. as
  9750.     declare @agent_type int
  9751.     declare @agent_name nvarchar(100)
  9752.     declare @status int
  9753.  
  9754.     -- Remove all existing instances
  9755.     dbcc deleteinstance ("SQL Replication Agents", "%")
  9756.     dbcc deleteinstance ("SQL Replication Snapshot", "%")
  9757.     dbcc deleteinstance ("SQL Replication Logreader", "%")
  9758.     dbcc deleteinstance ("SQL Replication Distribution", "%")
  9759.     dbcc deleteinstance ("SQL Replication Merge", "%")
  9760.  
  9761.     -- Add and initialize Perfmon SQL Replication Agents instances
  9762.     dbcc addinstance ("SQL Replication Agents", "Snapshot")
  9763.     dbcc addinstance ("SQL Replication Agents", "Logreader")
  9764.     dbcc addinstance ("SQL Replication Agents", "Distribution")
  9765.     dbcc addinstance ("SQL Replication Agents", "Merge")
  9766.     dbcc setinstance ("SQL Replication Agents", "Running", "Snapshot", 0)
  9767.     dbcc setinstance ("SQL Replication Agents", "Running", "Logreader", 0)
  9768.     dbcc setinstance ("SQL Replication Agents", "Running", "Distribution", 0)
  9769.     dbcc setinstance ("SQL Replication Agents", "Running", "Merge", 0)
  9770.  
  9771.     -- Add instances for each agent currently in the status table
  9772.     declare hC CURSOR LOCAL FAST_FORWARD for select agent_type, agent_name, status from tempdb.dbo.MSreplication_agent_status for read only
  9773.     open hC
  9774.     fetch hC into @agent_type, @agent_name, @status
  9775.     while (@@fetch_status <> -1)
  9776.     begin
  9777.  
  9778.         if @agent_type = 1 
  9779.         begin
  9780.             dbcc addinstance ("SQL Replication Snapshot", @agent_name)
  9781.             if @status = 1 or @status = 3 or @status = 4
  9782.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Snapshot", 1)
  9783.         end
  9784.         else if @agent_type = 2 
  9785.         begin
  9786.             dbcc addinstance ("SQL Replication Logreader", @agent_name)
  9787.             if @status = 1 or @status = 3 or @status = 4
  9788.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Logreader", 1)
  9789.         end
  9790.         else if @agent_type = 3 
  9791.         begin
  9792.             dbcc addinstance ("SQL Replication Distribution", @agent_name)
  9793.             if @status = 1 or @status = 3 or @status = 4
  9794.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Distribution", 1)
  9795.         end
  9796.         else if @agent_type = 4 
  9797.         begin
  9798.             dbcc addinstance ("SQL Replication Merge", @agent_name)
  9799.             if @status = 1 or @status = 3 or @status = 4
  9800.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Merge", 1)
  9801.         end
  9802.  
  9803.         fetch hC into @agent_type, @agent_name, @status
  9804.     end
  9805.     close hC
  9806.     deallocate hC
  9807. GO
  9808.  
  9809. raiserror('Creating procedure sp_MSrepl_startup', 0,1)
  9810. go
  9811. create proc sp_MSrepl_startup
  9812. as
  9813.  
  9814.     -- Drop and create publisher side cache table
  9815.     if exists (select * from tempdb..sysobjects where name = 'MSpublisher_access' and
  9816.     type = 'U')
  9817.         drop table tempdb.dbo.MSpublisher_access
  9818.  
  9819.     create table tempdb.dbo.MSpublisher_access
  9820.     (
  9821.     spid int NOT NULL,
  9822.     db_id int not null,
  9823.     publication sysname not null,
  9824.     login_time datetime not null,
  9825.     pubid uniqueidentifier null -- Used for merge only.
  9826.     )
  9827.  
  9828.     CREATE UNIQUE CLUSTERED INDEX ucMSpublisher_access ON tempdb.dbo.MSpublisher_access
  9829.         (spid, publication, db_id, login_time)
  9830.     
  9831.  
  9832.     -- Drop and create distributor side cache table
  9833.     -- We need to avoid publisher and distributor using the same table to prevent
  9834.     -- contention in local distributor case.
  9835.     if exists (select * from master..sysservers
  9836.                   WHERE  srvstatus & 8 <> 0 and UPPER(datasource) = UPPER(@@servername))
  9837.     begin
  9838.         if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  9839.         type = 'U')
  9840.             drop table tempdb.dbo.MSdistributor_access
  9841.  
  9842.         create table tempdb.dbo.MSdistributor_access
  9843.         (
  9844.         spid int NOT NULL,
  9845.         db_id int not null,
  9846.         agent_id int not null,
  9847.         agent_type int not null,
  9848.         publication_id int not null,
  9849.         login_time datetime not null
  9850.         )
  9851.  
  9852.         CREATE UNIQUE CLUSTERED INDEX ucMSdistributor_access ON tempdb.dbo.MSdistributor_access
  9853.             (spid, agent_id, db_id, login_time)
  9854.     end
  9855. go
  9856.  
  9857. -- If a distributor is installed, mark the sp as a startup sp. 
  9858. if exists (select * FROM master..sysservers WHERE  srvstatus & 8 <> 0)
  9859.     exec dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  9860. go
  9861.  
  9862. raiserror('Creating procedure sp_MSflush_access_cache', 0,1)
  9863. go
  9864. CREATE PROCEDURE sp_MSflush_access_cache 
  9865. AS
  9866.     -- Delete all the 'dead' connections in MSpublisher_access.
  9867.     delete tempdb.dbo.MSpublisher_access 
  9868.         from tempdb.dbo.MSpublisher_access a
  9869.         where not exists (select * from master..sysprocesses p where
  9870.             a.spid = p.spid and
  9871.             p.login_time = a.login_time)
  9872.     if @@error <> 0
  9873.         return 1
  9874.     else
  9875.         return 0
  9876.  
  9877.     if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  9878.     type = 'U')
  9879.     begin
  9880.         -- Delete all the 'dead' connections in MSdistributor_access.
  9881.         delete tempdb.dbo.MSdistributor_access 
  9882.             from tempdb.dbo.MSdistributor_access a
  9883.             where not exists (select * from master..sysprocesses p where
  9884.                 a.spid = p.spid and
  9885.                 p.login_time = a.login_time)
  9886.         if @@error <> 0
  9887.             return 1
  9888.         else
  9889.             return 0
  9890.     end
  9891.     return (0)
  9892. GO
  9893.  
  9894. raiserror('Creating procedure sp_MSreinit_failed_subscriptions', 0,1)
  9895. go
  9896. -- This stored procedure is used as a response to the Replication Validation Failure Alert.
  9897. -- It will reinit the failed subscription. If the publisher is remote, it must be configured as a remote server 
  9898. -- for this procedure to work.
  9899. create proc sp_MSreinit_failed_subscriptions
  9900. @failure_level int = 0      -- 0 All failure  1 Validation failures
  9901. as
  9902.  
  9903.     declare @publisher sysname
  9904.     declare @publisher_db sysname
  9905.     declare @publication sysname
  9906.     declare @article sysname
  9907.     declare @publication_type int
  9908.     declare @subscriber sysname
  9909.     declare @subscriber_db sysname
  9910.     declare @agent_type int
  9911.     declare @alert_id int
  9912.     declare @proc nvarchar(100)
  9913.     declare @message nvarchar(4000)
  9914.     declare @retcode int
  9915.     declare @found bit
  9916.     declare @return_value int
  9917.     
  9918.     set nocount on
  9919.  
  9920.     set @found = 0          -- set if cursor returns a row
  9921.     set @return_value = 0   -- set to success
  9922.  
  9923.     -- For each publication validation failure, resync the subscription
  9924.     declare hc CURSOR LOCAL FAST_FORWARD for select publisher, publisher_db, publication, publication_type, article, subscriber, 
  9925.         subscriber_db, alert_id from 
  9926.         msdb..sysreplicationalerts where
  9927.         (@failure_level = 0 or (@failure_level = 1 and alert_error_code = 20574)) and   -- 20574 = validation failure
  9928.         status = 0
  9929.         for read only
  9930.  
  9931.     open hc
  9932.     fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  9933.     while (@@fetch_status <> -1)
  9934.     begin
  9935.         
  9936.         set @found = 1
  9937.  
  9938.         -- Reinit snapshot or transactional subscription (article level)
  9939.         if @publication_type = 0 or @publication_type = 1
  9940.         begin
  9941.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitsubscription'
  9942.             exec @retcode = @proc
  9943.                 @publication = @publication,
  9944.                 @article = @article,
  9945.                 @subscriber = @subscriber,
  9946.                 @destination_db = @subscriber_db
  9947.             -- Ignore failures, update status bit if successful
  9948.             if @retcode = 0
  9949.             begin
  9950.                 -- Change status to 1 which means the alerts has been serviced
  9951.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  9952.  
  9953.                 -- Raiserror that subscription was reinitialized
  9954.                 if @failure_level = 0
  9955.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a synchronization failure.'
  9956.                     raiserror(20576, 10,-1, @subscriber, @article, @publication) 
  9957.                 else if @failure_level  = 1
  9958.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a validation failure.'
  9959.                     raiserror(20572, 10,-1, @subscriber, @article, @publication) 
  9960.             end
  9961.             else -- failure
  9962.                 set @return_value = 1
  9963.         end
  9964. /* Placeholder for Merge logic
  9965.         -- Reinit merge subscription (full publication)
  9966.         else if @publication_type = 3       
  9967.         begin
  9968.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitmergesubscription'
  9969.             exec @retcode = @proc
  9970.                 @publication = @publication,
  9971.                 @subscriber = @subscriber,
  9972.                 @subscriber_db = @subscriber_db
  9973.             -- Ignore failures, update status bit if successful
  9974.             if @retcode = 0
  9975.             begin
  9976.                 -- Change status to 1 which means the alerts has been serviced
  9977.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  9978.  
  9979.                 -- Raiserror that subscription was reinitialized
  9980.                 if @failure_level = 0
  9981.                     -- 'Subscriber ''%s'' subscription to publication ''%s'' has been reinitialized after a synchronization failure.'
  9982.                     raiserror(20576, 10,-1, @subscriber, @publication) 
  9983.                 else if @failure_level  = 1
  9984.                     -- 'Subscriber ''%s'' subscription to publication ''%s'' has been reinitialized after a validation failure.'
  9985.                     raiserror(20572, 10,-1, @subscriber, @publication) 
  9986.             end
  9987.             else -- failure
  9988.                 set @return_value = 1
  9989.         end
  9990. */
  9991.         fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  9992.     end
  9993.  
  9994.     close hc
  9995.     deallocate hc
  9996.  
  9997.     -- Return a message stating no entries where found
  9998.     if @found = 0
  9999.     begin
  10000.         -- 'No entries were found in msdb..sysreplicationalerts.'
  10001.         raiserror(20577, 10,-1) 
  10002.  
  10003.         -- There is most likely a problem, set failure return value
  10004.         set @return_value = 1
  10005.     end
  10006.  
  10007.     return @return_value 
  10008. go
  10009. dump tran master with no_log
  10010. GO
  10011.  
  10012. raiserror('Creating procedure sp_add_datatype_mapping', 0,1)
  10013. go
  10014.  
  10015. -- Add a row into the "MSdatatype_mappings" table
  10016. create procedure sp_add_datatype_mapping (
  10017.     @dbms_name          sysname,
  10018.     @sql_type           sysname,
  10019.     @dest_type          sysname,
  10020.     @dest_prec          int,
  10021.     @dest_create_params int,
  10022.     @dest_nullable      bit
  10023. )
  10024. as
  10025.     set nocount on
  10026.  
  10027.     if @dbms_name is null
  10028.     BEGIN
  10029.         RAISERROR (14043, 16, -1, '@dbms_name')
  10030.         RETURN (1)
  10031.     END
  10032.     
  10033.     if @sql_type is null 
  10034.     BEGIN
  10035.         RAISERROR (14043, 16, -1, '@sql_type')
  10036.         RETURN (1)
  10037.     END
  10038.  
  10039.     if @dest_type is null
  10040.     BEGIN
  10041.         RAISERROR (14043, 16, -1, '@dest_type')
  10042.         RETURN (1)
  10043.     END
  10044.  
  10045.     if @dest_prec is null
  10046.     BEGIN
  10047.         RAISERROR (14043, 16, -1, '@dest_prec')
  10048.         RETURN (1)
  10049.     END
  10050.  
  10051.     if @dest_create_params is null
  10052.     BEGIN
  10053.         RAISERROR (14043, 16, -1, '@dest_create_params')
  10054.         RETURN (1)
  10055.     END
  10056.  
  10057.     if @dest_nullable is null
  10058.     BEGIN
  10059.         RAISERROR (14043, 16, -1, '@dest_nullable')
  10060.         RETURN (1)
  10061.     END
  10062.  
  10063.  
  10064.     insert into msdb..MSdatatype_mappings
  10065.     values (@dbms_name, @sql_type, @dest_type, @dest_prec, @dest_create_params, @dest_nullable)
  10066.  
  10067.     if @@error <> 0
  10068.         return(1)
  10069. GO
  10070.  
  10071.  
  10072. raiserror('Creating procedure sp_help_datatype_mapping', 0,1)
  10073. go
  10074. create procedure sp_help_datatype_mapping(
  10075.     @dbms_name          sysname,
  10076.     @sql_type           sysname = '%',
  10077.     @source_prec        int = NULL
  10078. )
  10079. as
  10080.     set nocount on
  10081.     
  10082.     if @source_prec is NULL
  10083.     begin
  10084.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  10085.         from msdb..MSdatatype_mappings 
  10086.         where dbms_name = @dbms_name
  10087.         and sql_type like @sql_type
  10088.     end
  10089.     else
  10090.     begin
  10091.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  10092.         from msdb..MSdatatype_mappings 
  10093.         where dbms_name = @dbms_name
  10094.         and sql_type like @sql_type
  10095.         and dest_prec = (select min(dest_prec)
  10096.                             from msdb..MSdatatype_mappings 
  10097.                             where dbms_name = @dbms_name
  10098.                             and sql_type like @sql_type
  10099.                             and dest_prec >= @source_prec
  10100.                         )
  10101.     end
  10102. GO
  10103.  
  10104. raiserror('Creating procedure sp_MSfix_6x_tasks', 0,1)
  10105. go
  10106. CREATE PROCEDURE sp_MSfix_6x_tasks (
  10107.     @publisher sysname = NULL
  10108. ) AS
  10109.  
  10110.     SET NOCOUNT ON
  10111.  
  10112.     DECLARE @distributor sysname
  10113.     DECLARE @distproc nvarchar (255)
  10114.     declare @retcode int
  10115.  
  10116.     -- If @publisher is null redirect the call to distributor
  10117.     if @publisher is null
  10118.     begin
  10119.         /*
  10120.         ** Get distribution server information for remote RPC
  10121.         ** agent verification.
  10122.         */
  10123.         EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  10124.  
  10125.         IF @@error <> 0 OR @retcode <> 0
  10126.         BEGIN
  10127.             RAISERROR (14071, 16, -1)
  10128.             RETURN (1)
  10129.         END
  10130.  
  10131.         /*
  10132.         ** RPC distributor
  10133.         */
  10134.         SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_MSfix_6x_tasks'
  10135.  
  10136.         EXECUTE @retcode = @distproc
  10137.             @publisher = @@SERVERNAME
  10138.         IF @@ERROR <> 0 or @retcode <> 0
  10139.             return (1)
  10140.     end
  10141.     else
  10142.     begin
  10143.  
  10144.         declare @category_id int
  10145.         declare @category_name sysname
  10146.         declare @server sysname
  10147.         declare @databasename sysname
  10148.         declare @name sysname
  10149.         declare @distdb sysname
  10150.         declare @job_id uniqueidentifier
  10151.         declare @sSubsystem sysname
  10152.  
  10153.         -- Drop entry in systasks first.
  10154.         DECLARE hCtasks CURSOR LOCAL FAST_FORWARD FOR
  10155.             SELECT name FROM msdb.dbo.systasks_view st WHERE 
  10156.                 
  10157.                 -- drop distribution agents.
  10158.                 (st.name LIKE @publisher + '_' + '%' + '_' + '%' AND 
  10159.                 LOWER(subsystem) = 'distribution' AND
  10160.                 server = @@SERVERNAME) OR
  10161.                                 
  10162.                 (LOWER(subsystem) = 'logreader' AND
  10163.                 server = @publisher) OR 
  10164.                 
  10165.                 (LOWER(subsystem) = 'snapshot' AND
  10166.                 server = @publisher) OR
  10167.  
  10168.                 (st.name LIKE '%' + '_' + '%' + '_Cleanup' AND 
  10169.                 st.command LIKE '%' + 'sp_replcleanup' + '%' AND
  10170.                 LOWER(subsystem) = 'tsql')
  10171.             FOR READ ONLY
  10172.  
  10173.         OPEN hCtasks
  10174.         FETCH hCtasks INTO @name
  10175.  
  10176.         WHILE (@@fetch_status <> -1)
  10177.         BEGIN
  10178.             
  10179.             EXEC @retcode = msdb.dbo.sp_droptask @name = @name
  10180.             if @retcode <> 0 or @@error <> 0
  10181.                 return(1)
  10182.             FETCH hCtasks INTO @name
  10183.         END
  10184.  
  10185.         -- Now spin through each old replication job and fixup categories names
  10186.         declare hcJobsToFix CURSOR LOCAL FAST_FORWARD for 
  10187.                 select distinct j.job_id, j.name, s.subsystem, 
  10188.                     s.server, s.database_name
  10189.                     from msdb.dbo.sysjobs j left join msdb.dbo.sysjobsteps s
  10190.                         on j.job_id = s.job_id
  10191.                     where j.category_id = 0 and s.step_id = 1 and lower(s.subsystem) in ( 'snapshot', 'logreader', 'distribution' )
  10192.                 for read only
  10193.  
  10194.         open hcJobsToFix 
  10195.         fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  10196.             @server,@databasename
  10197.  
  10198.         while (@@fetch_status <> -1 )
  10199.         begin
  10200.             -- Note, have to make it a transaction, once the category_id is changed,
  10201.             -- the task will never be picked up again.
  10202.             begin tran 
  10203.  
  10204.                 -- Get Distribution category name (assumes category_id = 10)
  10205.                 select @category_id =
  10206.                     case lower(@sSubsystem) 
  10207.                         when 'snapshot' then 15
  10208.                         when 'distribution' then 10
  10209.                         when 'logreader' then 13
  10210.                         else 0
  10211.                     end
  10212.  
  10213.                 select @category_name = name FROM msdb.dbo.syscategories where category_id = @category_id
  10214.                 
  10215.                 -- raiserror( 'Would update %s to category %d based on subsystem value %s', -1, 10, @nJobName, @iCategory, @sSubsystem )
  10216.                 exec @retcode = msdb.dbo.sp_update_job @job_id = @job_id, 
  10217.                     @category_name = @category_name
  10218.                 if @retcode <> 0 or @@error <> 0
  10219.                     goto UNDO
  10220.  
  10221.  
  10222.                 -- Add the replication agent for monitoring
  10223.                 SELECT @distdb = distribution_db from msdb..MSdistpublishers where UPPER(name) = UPPER(@server)
  10224.                 IF (@category_id = 13) -- Logreader
  10225.                 BEGIN
  10226.                     SELECT @distproc = @distdb + '.dbo.sp_MSadd_logreader_agent'
  10227.                     EXECUTE @retcode = @distproc
  10228.                         @name = @name,
  10229.                         @publisher = @server,
  10230.                         @publisher_db = @databasename,
  10231.                         @publication = '',  
  10232.                         @local_job = 1,
  10233.                         @job_existing = 1,
  10234.                         @job_id = @job_id
  10235.  
  10236.                     IF (@retcode <> 0 or @@error<>0)
  10237.                         goto UNDO
  10238.                 END
  10239.                 ELSE 
  10240.                 IF (@category_id = 15) -- Snapshot
  10241.                 BEGIN
  10242.                     DECLARE @publication sysname
  10243.  
  10244.                     SELECT @publication = NULL
  10245.                     EXECUTE master.dbo.sp_MSget_publication_from_taskname
  10246.                                             @taskname = @name,
  10247.                                             @publisher = @server,
  10248.                                             @publisherdb = @databasename,
  10249.                                             @publication = @publication OUTPUT
  10250.  
  10251.                     IF (@publication IS NOT NULL )
  10252.                     BEGIN    
  10253.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_snapshot_agent'
  10254.  
  10255.                         EXECUTE @retcode = @distproc
  10256.                             @name = @name,
  10257.                             @publisher = @server,
  10258.                             @publisher_db = @databasename,
  10259.                             @publication = @publication,
  10260.                             @local_job = 1,
  10261.                             @job_existing = 1,
  10262.                             @snapshot_jobid = @job_id
  10263.  
  10264.                         IF (@retcode <> 0 or @@error<>0)
  10265.                             goto UNDO
  10266.                 
  10267.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_publication'
  10268.                         EXECUTE @retcode = @distproc
  10269.                             @publisher = @server,
  10270.                             @publisher_db = @databasename,
  10271.                             @publication = @publication,
  10272.                             @publication_type = 0 -- Transactional
  10273.                         IF (@retcode <> 0 or @@error<>0)
  10274.                             goto UNDO
  10275.                     END
  10276.                 END
  10277.             commit tran
  10278.             fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  10279.                 @server,@databasename
  10280.         end            
  10281.         close hcJobsToFix
  10282.         deallocate hcJobsToFix
  10283.     end
  10284.     return (0)
  10285. UNDO:
  10286.     if @@trancount <> 0
  10287.         rollback tran
  10288.     return (1)
  10289. GO
  10290.  
  10291. /*
  10292. ** Add extended stored procedures for replication support.
  10293. */
  10294. sp_addextendedproc  'sp_repldone', 'repldone extended procedure'
  10295. go
  10296.  
  10297. sp_addextendedproc  'sp_repltrans', 'repltrans extended procedure'
  10298. go
  10299.  
  10300. sp_addextendedproc  'sp_replcounters', 'replcounters extended procedure'
  10301. go
  10302.  
  10303. sp_addextendedproc  'sp_replcmds', 'replcmds extended procedure'
  10304. go
  10305.  
  10306. sp_addextendedproc  'sp_replflush', 'replflush extended procedure'
  10307. go
  10308.  
  10309. dump tran master with no_log
  10310. go
  10311.  
  10312. sp_addextendedproc  'sp_replpostcmd', 'replpostcmd extended procedure'
  10313. go
  10314.  
  10315. sp_addextendedproc  'sp_replincrementlsn', 'replincrementlsn extended procedure'
  10316. go
  10317.  
  10318. sp_addextendedproc  'sp_replupdateschema', 'replupdateschema extended procedure'
  10319. go
  10320.  
  10321. sp_addextendedproc  'sp_replsetoriginator', 'replsetoriginator extended procedure'
  10322. go
  10323.  
  10324. /*
  10325. ** Add xp_enum_dsn extended procedure
  10326. */
  10327. sp_addextendedproc 'xp_dsninfo','xpsql70.dll'
  10328. go
  10329. exec dbo.sp_MS_marksystemobject xp_dsninfo
  10330. go
  10331.  
  10332. /*
  10333. ** Add xp_enum_dsn extended procedure
  10334. */
  10335. sp_addextendedproc 'xp_enumdsn','xpsql70.dll'
  10336. go
  10337. exec dbo.sp_MS_marksystemobject xp_enumdsn
  10338. go
  10339.  
  10340. /*
  10341. ** Add xp_oledbinfo extended procedure
  10342. */
  10343. sp_addextendedproc 'xp_oledbinfo','xprepl.dll'
  10344. go
  10345. exec dbo.sp_MS_marksystemobject xp_oledbinfo
  10346. go
  10347.  
  10348. /*
  10349. ** Add xp_repl_encrypt extended procedure
  10350. */
  10351. sp_addextendedproc 'xp_repl_encrypt','xprepl.dll'
  10352. go
  10353. exec dbo.sp_MS_marksystemobject xp_repl_encrypt
  10354. go
  10355.  
  10356.  
  10357. dump tran master with no_log
  10358. go
  10359.  
  10360. grant execute on dbo.sp_addpublication_snapshot to public
  10361. go
  10362. grant execute on dbo.sp_MShelpobjectpublications to public
  10363. go
  10364. grant execute on dbo.sp_helpreplicationdb to public
  10365. go
  10366. grant execute on dbo.sp_enumdsn to public
  10367. go
  10368. grant execute on dbo.sp_helpsubscriberinfo to public
  10369. go
  10370. grant execute on dbo.sp_replica to public
  10371. go
  10372. grant execute on dbo.sp_distcounters to public
  10373. go
  10374. grant execute on dbo.sp_helpdistributor to public
  10375. go
  10376. grant execute on dbo.sp_helpdistributiondb to public
  10377. go
  10378. grant execute on dbo.sp_helpdistpublisher to public
  10379. go
  10380. grant execute on dbo.sp_replcounters to public
  10381. go
  10382. grant execute on dbo.sp_MShelp_distdb to public
  10383. go
  10384. grant execute on dbo.sp_grant_publication_access to public
  10385. go
  10386. grant execute on dbo.sp_revoke_publication_access to public
  10387. go
  10388. grant execute on dbo.sp_help_publication_access to public
  10389. go
  10390. grant execute on dbo.sp_check_publication_access to public
  10391. go
  10392. grant execute on dbo.sp_replsetoriginator to public
  10393. go
  10394.  
  10395. grant execute on dbo.sp_repldone to public
  10396. go
  10397. grant execute on dbo.sp_repltrans to public
  10398. go
  10399. grant execute on dbo.sp_replcounters to public
  10400. go
  10401. grant execute on dbo.sp_replcmds to public
  10402. go
  10403. grant execute on dbo.sp_replpostcmd to public
  10404. go
  10405. grant execute on dbo.sp_replincrementlsn to public
  10406. go
  10407. grant execute on sp_MSenum_replication_job to public
  10408. go
  10409.  
  10410.  
  10411. dump tran master with no_log
  10412. go
  10413.  
  10414.  
  10415.  
  10416. dump tran master with no_log
  10417. go
  10418. sp_configure 'allow updates',0
  10419. go
  10420. reconfigure with override
  10421. go
  10422.  
  10423. print ''
  10424. print 'Checking objects created by replcom.sql.'
  10425. go
  10426. --obsolete   exec dbo.sp_check_objects 'repl'
  10427. exec dbo.sp_MS_upd_sysobj_category 2  --set sysobjects.category | 2 based on crdate.
  10428. go
  10429.  
  10430. print ''
  10431. print 'replcom.sql completed successfully.'
  10432. go
  10433.  
  10434. dump tran master with no_log
  10435. go
  10436. checkpoint
  10437. go
  10438. -- - -----
  10439.